duanmen8491 2012-01-15 00:11
浏览 33
已采纳

在多维数组数组中查找值

The following function retrieves a list of friends of the active user who are majoring in a given subject in college. The data is pulled from the Facebook GRAPH API. (THIS FUNCTION WORKS)...

 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>';
      }

Now I'm working on a function to return a list of friends who work for the same company, hold the same job position, etc. Here is my unsuccessful stab at the function:

  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>';
      }

This solution just returns an empty array but the test user does have friends who meet the requirements of the function. Below is the array of a user's friends list. Any help with a proposed solution would be greatly appreciated. I'm on day three now of this problem:

(NOTE: Each [OFFSET] is a different friend of the active user:

 [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条回答 默认 最新

  • douxiaochun4964 2012-01-15 03:14
    关注

    var_dump for every variable until you find an empty one, such as below Assuming your facebook connect stuff is all working properly try the code below, this should get you some useful output.

    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>';
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线