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 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿