Matthieu McLaren
Matthieu McLaren

Reputation: 224

Looping through a multidimensional array to find certain values

The goal here is to loop through the multidimensional array below and find user's who live in a certain city. I only pasted a small part of the array so you could get a feel for the structure. It comes from the Facebook Graph API. This function below shoots back this error message: Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\pathweavr\friendtest.php on line 143

Line 143:

    foreach ($location['city'] as $city) {      

And here's the code:

    $friends = $fqlResult;
    $friends_BA = array();
    foreach ($friends as $friend) {
    $isBA = false;
       if (is_array($friend['current_location'])) {
          foreach ($friend['current_location'] as $location) {
             if (isset($location)) {
                foreach ($location['city'] as $city) {
                   $lowerName = strtolower($city);
                   if (strpos($lowerName, 'orlando') !== false || strpos($lowerName, 'gainesville') !== false) {
                   $friends_BA[] = $friend['name'];
                   continue 3; // skip to the next friend
                   }
                 }
               }
            }
         }
      }

    d($friends_BA);

The array looks like this:

 Array
(
[0] => Array
(
    [name] => PERSONS NAME
    [current_location] => Array
        (
            [city] => New York
            [state] => New York
            [country] => United States
            [zip] => 
            [id] => 108424279189115
            [name] => New York, New York
        )

)

 [1] => Array
(
    [name] => PERSONS NAME
    [current_location] => 
)

 [2] => Array
(
    [name] => PERSONS NAME
    [current_location] => 
)

 [3] => Array
(
    [name] => PERSONS NAME
    [current_location] => 
)

 [4] => Array
(
    [name] => PERSONS NAME
    [current_location] => Array
        (
            [city] => San Jose
            [state] => California
            [country] => United States
            [zip] => 
            [id] => 111948542155151
            [name] => San Jose, California
        )

)

 [5] => Array
(
    [name] => PERSONS NAME
    [current_location] => Array
        (
            [city] => Boston
            [state] => Massachusetts
            [country] => United States
            [zip] => 
            [id] => 106003956105810
            [name] => Boston, Massachusetts
        )

)

Been playing around with it for an hour but can't seem to make it work. I'm getting invalid arguments on the second foreach statement.

Upvotes: 0

Views: 547

Answers (3)

mask8
mask8

Reputation: 3638

you don't need 2 foreach's

$friends = $fqlResult;
$friends_BA = array();
foreach ($friends as $friend) {
  $isBA = false;
  if (is_array($friend['current_location'])) {
    $lowerName = strtolower($friend['current_location']['city']);
    if (strpos($lowerName, 'orlando') !== false || strpos($lowerName, 'gainesville') !== false) {
      $friends_BA[] = $friend['name'];
    }
  }
}

d($friends_BA);

Upvotes: 1

Indranil
Indranil

Reputation: 2471

This is $location['city'] is not an array. It's a string variable. foreach only loops through arrays.

Upvotes: 1

Jonathan M
Jonathan M

Reputation: 17451

$location['city'] is never an array in your data structure. You're trying to iterate over a scalar.

Upvotes: 0

Related Questions