colinreedy674
colinreedy674

Reputation: 365

Warning error on JSON decode

I'm getting a warning on a json decode foreach within a foreach (although the code works which is strange) the warning is: Warning: Invalid argument supplied for foreach() it is referring to this line: foreach ($value as $val) {

Here is the JSON response:

Array
(
    [ACTION] => avail.datacenters
    [DATA] => Array
        (
            [0] => Array
                (
                    [LOCATION] => Dallas, TX, USA
                    [DATACENTERID] => 2
                    [ABBR] => dallas
                )

            [1] => Array
                (
                    [LOCATION] => Fremont, CA, USA
                    [DATACENTERID] => 3
                    [ABBR] => fremont
                )

            [2] => Array
                (
                    [LOCATION] => Atlanta, GA, USA
                    [DATACENTERID] => 4
                    [ABBR] => atlanta
                )

            [3] => Array
                (
                    [LOCATION] => Newark, NJ, USA
                    [DATACENTERID] => 6
                    [ABBR] => newark
                )

            [4] => Array
                (
                    [LOCATION] => London, England, UK
                    [DATACENTERID] => 7
                    [ABBR] => london
                )

            [5] => Array
                (
                    [LOCATION] => Tokyo, JP
                    [DATACENTERID] => 8
                    [ABBR] => tokyo
                )

            [6] => Array
                (
                    [LOCATION] => Singapore, SG
                    [DATACENTERID] => 9
                    [ABBR] => singapore
                )

            [7] => Array
                (
                    [LOCATION] => Frankfurt, DE
                    [DATACENTERID] => 10
                    [ABBR] => frankfurt
                )

            [8] => Array
                (
                    [LOCATION] => Tokyo 2, JP
                    [DATACENTERID] => 11
                    [ABBR] => shinagawa1
                )

        )

    [ERRORARRAY] => Array
        (
        )

)

My foreach code:

                $randDCID = array();
                foreach ($linodeRegions as $value) {
                    foreach ($value as $val) {
                        echo $val['DATACENTERID'] . "<br />";
                        $randDCID[] = $val['DATACENTERID'];
                    }                           
                }

Can anyone see the issue on the warning (although it is outputting the desired results).

Upvotes: 0

Views: 54

Answers (1)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

The error is probably coming from the ACTION index and the corresponding value(which is string) of the array. Assuming the fact that $linodeRegions in your original array, there's no need to create nested loops in this case, simply use a foreach loop like this:

$randDCID = array();
foreach ($linodeRegions['DATA'] as $value) {
    echo $value['DATACENTERID'] . "<br />";
    $randDCID[] = $value['DATACENTERID'];                         
}

Upvotes: 1

Related Questions