2 Replies - 2188 Views - Last Post: 13 January 2012 - 05:13 PM Rate Topic: -----

#1 mattrmclaren   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Finding values in a multidimensional array

Posted 13 January 2012 - 10:53 AM

Below is one offset of a multidimensional array I'm pulling from the Facebook Graph API which features a list of a user's friends. I was able to use the following code to find any friends of the active user that live in a given city:

function getLocalFriends () {
                 $config = array(
                 'appId' => 'xxxxxxxx',
                 'secret' => 'xxxxxxx',
                 );
                 $facebook = new Facebook($config);
                 $user_id = $facebook->getUser();
                 try {
                 $fql    =   "select name,current_location 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['current_location'])) {
                      $lowerName = strtolower($friend['current_location']['city']);
                          if (strpos($lowerName, 'orlando') !== false || strpos($lowerName, 'altamonte springs') !== false) {
                          $friends_BA[] = $friend['name'];
                }
             }
           }
                 echo '<pre>';
                 print_r($friends_BA);
                 echo '</pre>';
    }


This function was successful on the array and brought me back the appropriate list. Now I am trying to run a similar function that determines if a friend of the user who works at a particular company or holds a certain job title. Here's what I've come up with, it returns nothing but an empty array:

function getWorkFriends () {
                 $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'])) {
                    $lowerName = strtolower($friend['work_history']['company_name']);
                       if (strpos($lowerName, 'UBS') !== false || strpos($lowerName, 'Merion') !== false) {
                       $friends_BA[] = $friend['name'];
                      }
                    }
                 }
                 echo '<pre>';
                 print_r($friends_BA);
                 print_r($fqlresult);
                 print_r($friend['work_history']);
                 echo '</pre>';

                 }


I have spent a lot of time trying to figure out what's wrong but I'm at a loss. Any help would be sincerely appreciated! Every friend has an offset, like [12]

 array (  [12] => Array
        (
            [uid] => 2058060
            [name] => Persons Name
            [current_location] => Array
                (
                    [city] => Boca Raton
                    [state] => Florida
                    [country] => United States
                    [zip] => 
                    [id] => 105681686132068
                    [name] => Boca Raton, Florida
                )

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

                            [company_name] => Alliance Residential Company
                            [position] => Project Engineer
                            [description] => 
                            [start_date] => 0000-00
                        )

                    [1] => Array
                        (
                            [location] => Array
                                (
                                    [city] => Fort Lauderdale
                                    [state] => Florida
                                )

                            [company_name] => Moss & Associates
                            [position] => Project Engineer
                            [description] => 
                            [start_date] => 0000-00
                            [end_date] => 0000-00
                        )

                )
)
        )

This post has been edited by mattrmclaren: 13 January 2012 - 10:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Finding values in a multidimensional array

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5283
  • View blog
  • Posts: 14,104
  • Joined: 18-April 07

Re: Finding values in a multidimensional array

Posted 13 January 2012 - 12:45 PM

work_history is an array of arrays. In each of those arrays is the company_name.

So if I am reading this right you would get at it using $friend['work_history'][0]['company_name'].

So access the first array of work_history (element 0) and then get the company name out of it. Perhaps this will solve your problem. :)

This post has been edited by Martyr2: 13 January 2012 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 mattrmclaren   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Finding values in a multidimensional array

Posted 13 January 2012 - 05:13 PM

I added the 0 offset and still get an empty array send back like so:

Array
(
)


This is the change I made:

$friends = $fqlResult;
                 $friends_BA = array();
                 foreach ($friends as $friend) {
                 $isBA = false;
                    if (is_array($friend['work_history'])) {
                    $lowerName = strtolower($friend['work_history'][0]['company_name']);
                       if (strpos($lowerName, 'UBS') !== false || strpos($lowerName, 'Merion') !== false) {
                       $friends_BA[] = $friend['name'];
                      }
                    }
                 }
                 echo '<pre>';
                 print_r($friends_BA);
                 echo '</pre>';

                 }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1