PHP sorting thru Nested loops question

[ad_1]

Hello all,

So I’m trying to sort thru nested data from an API and insert it into the WordPress MySQL database. I’ve already created my SQL table, and I’ve succesfully pushed API data to it using a simpler test loop.

However, when I try to access all the levels of the JSON data using a big ol’ foreach loop, I’m getting nothing in the database:

$results = wp_remote_retrieve_body(wp_remote_get( $url, $args ));

$res = json_decode($results);

$odds = [];

foreach ($res as $odd) {
foreach ($odd->bookmakers as $bm) {
foreach ($bm->markets as $market) {
foreach ($market->outcomes as $outcome) {
$odds = [
‘key_id’ => $odd->id,
‘home_team’ => $odd->home_team,
‘away_team’ => $odd->away_team,
‘commence_time’ => $bm->commence_time,
‘sport_key’ => $odd->sport_key,
‘last_updated_at’ => $bm->last_update,
‘bookmaker_key’ => $bm->key,
‘market’ => $market->key,
‘label’ => $outcome->name,
‘price’ => $outcome->price,
‘points’ => $outcome->point
];
}
}
}

#Insert data into MySQL table
global $wpdb;

$table_name = $wpdb->prefix . ‘game_odds’;

$wpdb->insert(
$table_name,
$odds
);
}

Meanwhile this code works fine and pushes data to my database:

$results = wp_remote_retrieve_body(wp_remote_get( $url, $args ));

$res = json_decode($results);

$test_odds = [];

foreach ($res as $odd) {
$test_odds = [
‘key_id’ => $odd->id,
‘home_team’ => $odd->home_team,
‘away_team’ => $odd->away_team,
‘sport_key’ => $odd->sport_key
];

#Insert data into MySQL table
global $wpdb;

$table_name = $wpdb->prefix . ‘game_odds’;

$wpdb->insert(
$table_name,
$test_odds
);
}

Any help is appreciated, thanks!

[ad_2]
5 Comments
  1. You’re gonna have to insert some debug statements in the version that isn’t working to see if those variables are populated with the data you expect.

    For example have you tried doing a print_r($odds) above the one that isn’t working to see if it has data?

  2. I don’t have you API format/docs etc… but I suspect you have to get each item in it’s loop…
    foreach ($res as $odd) {
    // get your first level items here as with other code (e.g. ‘key_id’ => $odd->id)
    foreach ($odd->bookmakers as $bm) {
    // get second level items here (e.g. ‘last_updated_at’ => $bm->last_update)
    foreach ($bm->markets as $market) {
    / third level items
    foreach ($market->outcomes as $outcome) {
    // because by here you can’t ‘reach back’ for the items up the json tree

    That’s one possibility. The other is that you need to build several arrays. If there are multiple markets, outcomes etc. then you will have:

    $odds[0] -> $bookmakers[0] -> $markets[0] (etc.)
    ↳ $markets[1] (and 2 and 3 etc.)
    ↳ $bookmakers[1] -> $markets[0]

    and so on, so arrays nested in arrays to collect all your data. At present every ‘each’ will simply overwrite the previous item.

    Otherwise – you should foreach your $res as $odds, get all the sub-fields by direct access (e.g. [bookmakers][commence_time] – for each $odds – update the table with that one row of info, then NEXT your loop to end up with one row per $odds

  3. Firstly you will only end up with 1 array element there before inserting and it’s continuously overwriting itself through each iteration.

  4. See what you’ve got at each level.

    `echo ‘<pre>’;`

    `print_r($odd);`

    `echo ‘</pre>’;`

    etc.

    Also, wrap your insert into a conditional

    `if (!$wpdb->insert($table_name, $test_ods)) {`

    `echo ‘Couldn’t insert data: ‘ . $wpdb->last_error . ‘, last query was: ‘ . $spdb->last_query . ‘<br />’;`

    `}`

  5. I really recommend doing this kind of programming locally with an IDE and proper debugging. It generally makes figuring out this sort of thing fast and easy. But if you have to do it on the server, you can write debug output statements to show the data structures and just keep drilling your way down. Others have given examples.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer