在多维数组数组中查找值

时间:2012-01-15 00:11:55

标签: php arrays multidimensional-array

以下函数检索在大学中主修特定主题的活跃用户的朋友列表。数据来自Facebook GRAPH API。 (这个功能工作)......

 function getCollegeFriends () {
       $config = array(
       'appId' => 'XXXXXXXXXXXXX',
       'secret' => 'XXXXXXXXXXXXXX',
       );
       $facebook = new Facebook($config);
       $user_id = $facebook->getUser();
       try {
       $fql    =   "select uid,name,education from user WHERE uid IN (select uid2 from friend where uid1=($user_id))";
       $param  =   array(
       'method'    => 'fql.query',
       'query'     => $fql,
       'callback'  => ''
       );
       $fqlResult   =   $facebook->api($param);
       }
       catch(Exception $o){
       d($o);
       }

       $friends = $fqlResult;
       $friends_BA = array();
       foreach ($friends as $friend) {
       $isBA = false;
           if (is_array($friend['education'])) {
              foreach ($friend['education'] as $school) {
                 if (isset($school)) {
                    foreach ($school['school'] as $name) {
                       $lowerName = strtolower($name);
                       if (strpos($lowerName, 'university of central florida') !== false || strpos($lowerName, 'ucf') !== false) {
                       $friends_BA[] = $friend['name'];
                       continue 3; // skip to the next friend


                       }
                     }
                   }
                }
             }
          }
             echo '<pre>';
             print_r($friends_BA);
             echo '</pre>';
      }

现在我正在开发一个函数来返回为同一家公司工作的朋友列表,保持相同的工作岗位等。这是我对该功能的不成功:

  function getWorkerFriends () {
       $config = array(
       'appId' => 'XXXXXXXXX',
       'secret' => 'XXXXXXXXX',
       );
       $facebook = new Facebook($config);
       $user_id = $facebook->getUser();
       try {
       $fql    =   "select uid,name,work_history from user WHERE uid IN (select uid2 from friend where uid1=($user_id))";
       $param  =   array(
       'method'    => 'fql.query',
       'query'     => $fql,
       'callback'  => ''
       );
       $fqlResult   =   $facebook->api($param);
       }
       catch(Exception $o){
       d($o);
       }

       $friends = $fqlResult;
       $friends_BA = array();
       foreach ($friends as $friend) {
       $isBA = false;
           if (is_array($friend['work_history'])) {
              foreach ($friend['work_history'] as $employer) {
                 if (isset($employer)) {
                    foreach ($employer['location'] as $name) {
                       $lowerName = strtolower($name['company_name']);
                       if (strpos($lowerName, 'ubs') !== false || strpos($lowerName, 'merion') !== false) {
                       $friends_BA[] = $friend['name'];
                       continue 3; // skip to the next friend


                       }
                     }
                   }
                }
             }
          }
             echo '<pre>';
             print_r($friends_BA);
             echo '</pre>';
      }

此解决方案只返回一个空数组,但测试用户确实有满足该功能要求的朋友。下面是用户朋友列表的数组。任何有关建议解决方案的帮助将不胜感激。我现在正处于这个问题的第三天:

(注意:每个[OFFSET]是活跃用户的不同朋友:

 [124] => Array
    (
        [uid] => 553325624
        [name] => Persons Name
        [current_location] => Array
            (
                [city] => Los Angeles
                [state] => California
                [country] => United States
                [zip] => 
                [id] => 110970792260960
                [name] => Los Angeles, California
            )

        [work_history] => Array
            (
            )

        [education] => Array
            (
                [0] => Array
                    (
                        [school] => Array
                            (
                                [id] => 111894272160018
                                [name] => Spanish River Community High School
                            )

                        [type] => High School
                    )

                [1] => Array
                    (
                        [school] => Array
                            (
                                [id] => 110186095670702
                                [name] => North Broward Preparatory Schools
                            )

                        [type] => High School
                    )

            )

    )

[125] => Array
    (
        [uid] => 560613217
        [name] => Persons Name
        [current_location] => Array
            (
                [city] => Summit
                [state] => New Jersey
                [country] => United States
                [zip] => 
                [id] => 103727996333163
                [name] => Summit, New Jersey
            )

        [work_history] => Array
            (
                [0] => Array
                    (
                        [location] => Array
                            (
                                [city] => 
                                [state] => 
                            )

                        [company_name] => Camp High Rocks
                        [description] => 
                        [start_date] => 
                        [end_date] => 
                    )

            )

        [education] => Array
            (
                [0] => Array
                    (
                        [school] => Array
                            (
                                [id] => 106165586082302
                                [name] => Summit Senior High School
                            )

                        [type] => High School
                    )

                [1] => Array
                    (
                        [school] => Array
                            (
                                [id] => 6192688417
                                [name] => Stanford University
                            )

                        [year] => Array
                            (
                                [id] => 105576766163075
                                [name] => 2015
                            )

                        [type] => College
                    )

            )

    )

1 个答案:

答案 0 :(得分:0)

每个变量的var_dump,直到找到一个空变量,如下所示 假设你的facebook连接正常,请尝试下面的代码,这应该会给你一些有用的输出。

function getWorkerFriends () {
   $friends = $fqlResult;

   echo "\$friends <br/>";
   var_dump( $friends );

   $friends_BA = array();
   foreach ($friends as $friend) {
   $isBA = false;

        echo "\$friend <br />";
        var_dump( $friend );


       if (is_array($friend['work_history'])) {
          foreach ($friend['work_history'] as $employer) {

              echo "\$employer <br />";
              var_dump( $employer );

             if (isset($employer)) {
                foreach ($employer['location'] as $name) {

                    echo "\$name <br />";
                    var_dump( $name );

                   $lowerName = strtolower($name['company_name']);
                   if (strpos($lowerName, 'ubs') !== false || strpos($lowerName, 'merion') !== false) {
                   $friends_BA[] = $friend['name'];

                   echo "Continue to next friend! <br />";

                   continue 3; // skip to the next friend


                   }
                 }
               }
            }
         }
      }
         echo '<pre>';
         print_r($friends_BA);
         echo '</pre>';
  }