dpf25323 2011-12-20 02:48
浏览 58
已采纳

循环通过多维数组来查找某些值

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.

  • 写回答

3条回答 默认 最新

  • du9826 2011-12-20 03:07
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?