Looping through a multidimensional array

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 3208 Views - Last Post: 23 April 2012 - 02:54 PM Rate Topic: -----

#1 mattrmclaren   User is offline

  • D.I.C Head

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

Looping through a multidimensional array

Posted 19 December 2011 - 11:55 AM

I'm using the following code to loop through a multidimensional array and find users that went to a certain university, in this case UCF.

$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) {
                    if (strpos(strtolower($name['name']), 'University of Central Florida') !== false) {
                        $friends_BA[] = $friend['name'];
                        continue 3; // skip to the next friend
                    }
                }
            }
        }
    }
}

d($friends_BA);


The multidimensional array looks like this. It's a list of friends from the Facebook Graph API:

Array
(
    [0] => Array
        (
            [name] => PERSON
            [education] => 
        )

    [1] => Array
        (
            [name] => PERSON
            [education] => Array
                (
                    [0] => Array
                        (
                            [school] => Array
                                (
                                    [id] => 108087985890571
                                    [name] => St. Andrew's School
                                )

                            [year] => Array
                                (
                                    [id] => 138383069535219
                                    [name] => 2005
                                )

                            [type] => High School
                        )

                    [1] => Array
                        (
                            [school] => Array
                                (
                                    [id] => 20697868961
                                    [name] => Boston University
                                )

                            [concentration] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 108654845832522
                                            [name] => Business Administration
                                        )

                                )

                            [type] => College
                        )

                    [2] => Array
                        (
                            [school] => Array
                                (
                                    [id] => 108289315859633
                                    [name] => University of Miami
                                )

                            [year] => Array
                                (
                                    [id] => 138879996141011
                                    [name] => 2013
                                )

                            [type] => Graduate School
                        )

                )

        )

    [2] => Array
        (
            [name] => PERSON
            [education] => Array
                (
                    [0] => Array
                        (
                            [school] => Array
                                (
                                    [id] => 115444241803885
                                    [name] => Saint Andrews High School
                                )

                            [year] => Array
                                (
                                    [id] => 137616982934053
                                    [name] => 2006
                                )

                            [type] => High School
                        )

                    [1] => Array
                        (
                            [school] => Array
                                (
                                    [id] => 112033702149888
                                    [name] => Boca Raton High
                                )

                            [year] => Array
                                (
                                    [id] => 137616982934053
                                    [name] => 2006
                                )

                            [type] => High School
                        )

                    [2] => Array
                        (
                            [school] => Array
                                (
                                    [id] => 108087985890571
                                    [name] => St. Andrew's School
                                )

                            [type] => High School
                        )

                    [3] => Array
                        (
                            [school] => Array
                                (
                                    [id] => 107573562605861
                                    [name] => Duke University
                                )

                            [concentration] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 104045469631213
                                            [name] => Political science
                                        )

                                )

                            [type] => College
                        )

                )

        )

    [3] => Array
        (
            [name] => PERSON
            [education] => 
        )

    [4] => Array
        (
            [name] => PERSON
            [education] => Array
                (
                    [0] => Array
                        (
                            [school] => Array
                                (
                                    [id] => 106039752760627
                                    [name] => Berwick Academy
                                )

                            [year] => Array
                                (
                                    [id] => 137616982934053
                                    [name] => 2006
                                )

                            [type] => High School
                        )

                    [1] => Array
                        (
                            [school] => Array
                                (
                                    [id] => 108087985890571
                                    [name] => St. Andrew's School
                                )

                            [type] => High School
                        )

                    [2] => Array
                        (
                            [school] => Array
                                (
                                    [id] => 105690226130720
                                    [name] => Northeastern University
                                )

                            [concentration] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 108654845832522
                                            [name] => Business Administration
                                        )

                                )

                            [type] => College
                            [classes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 189873264368867
                                            [name] => 2011
                                        )

                                )

                        )

                )

        )


I've run a similar program to find user's majoring in certain subjects that did work, but for some reason the code above doesn't work. Here's the one that is working:

$friends = $fqlResult;
$friends_BA = array();

foreach ($friends as $friend) {
    $isBA = false;
    if (is_array($friend['education'])) {
        foreach ($friend['education'] as $school) {
            if (isset($school['concentration'])) {
                foreach ($school['concentration'] as $concentration) {
                    if (strpos(strtolower($concentration['name']), 'business') !== false) {
                        $friends_BA[] = $friend['name'];
                        continue 3; // skip to the next friend
                    }
                }
            }
        }
    }
}

d($friends_BA);


As always, any help would be sincerely appreciated. Happy Holidays!

Is This A Good Question/Topic? 0
  • +

Replies To: Looping through a multidimensional array

#2 jonesa01   User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 125
  • Joined: 18-February 07

Re: Looping through a multidimensional array

Posted 19 December 2011 - 12:07 PM

Just had a quick read through the code and to me it appears that the code in lines 11 is missing a count value

$friends_BA[] = $friend['name'];


I think you need something like the following:

$friends = $fqlResult;
$friends_BA = array();
$nfriends = 0;

foreach ($friends as $friend) {
    $isBA = false;
    if (is_array($friend['education'])) {
        foreach ($friend['education'] as $school) {
            if (isset($school['concentration'])) {
                foreach ($school['concentration'] as $concentration) {
                    if (strpos(strtolower($concentration['name']), 'business') !== false) {
                        $friends_BA[$nfriends] = $friend['name'];
                        $nfriends++;
                        continue 3; // skip to the next friend
                    }
                }
            }
        }
    }
}

d($friends_BA);




Apart from this I do not notice anything wrong with the code, just make sure all the array declarations are correct.
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: Looping through a multidimensional array

Posted 19 December 2011 - 12:14 PM

The second function you referenced does work, it's the first one that isn't working. The code that looks for a UCF student doesn't work, the one that looks for a business major does work. Either way I added the friend counter and still have no output.
Was This Post Helpful? 0
  • +
  • -

#4 jonesa01   User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 125
  • Joined: 18-February 07

Re: Looping through a multidimensional array

Posted 19 December 2011 - 01:12 PM

Try this at the after $friends = $fqlResult;:

echo "<pre>";
print_r($friends);
echo "</pre>";



This will tell you if the array exists and the contents of it. Then if there is no problem if you can post an example of it here we can help you further

This post has been edited by jonesa01: 19 December 2011 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 mattrmclaren   User is offline

  • D.I.C Head

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

Re: Looping through a multidimensional array

Posted 19 December 2011 - 01:24 PM

It prints a full list of all of the user's friends, just like the snippet of the MD array I posted above.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,800
  • Joined: 08-August 08

Re: Looping through a multidimensional array

Posted 19 December 2011 - 01:37 PM

Show us the code that populates the $fqlResult array.
Was This Post Helpful? 0
  • +
  • -

#7 mattrmclaren   User is offline

  • D.I.C Head

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

Re: Looping through a multidimensional array

Posted 19 December 2011 - 01:51 PM

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

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,800
  • Joined: 08-August 08

Re: Looping through a multidimensional array

Posted 19 December 2011 - 02:22 PM

So you're getting the array from an object. It's hard to believe that object doesn't have methods for drilling down into the array. What is the class definition for $facebook?
Was This Post Helpful? 0
  • +
  • -

#9 mattrmclaren   User is offline

  • D.I.C Head

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

Re: Looping through a multidimensional array

Posted 19 December 2011 - 02:28 PM

LOL. I know we always have this debate but no one on Stack Overflow for Facebook seems to know how to do it, using FQL or the API itself. People just call these huge multidimensional arrays and break them down themselves. Trust me, I've tried other ways. The day someone figures out how I will send them a bundle of cash (and by bundle I mean $50 or so).
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,800
  • Joined: 08-August 08

Re: Looping through a multidimensional array

Posted 19 December 2011 - 02:33 PM

In order to use:
$facebook->api($param);

you have to have at some point something like:
$facebook = new someclass();

and someclass needs to be defined, possibly using an include. Post that definition.
Was This Post Helpful? 0
  • +
  • -

#11 mattrmclaren   User is offline

  • D.I.C Head

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

Re: Looping through a multidimensional array

Posted 19 December 2011 - 02:51 PM

require_once('php-sdk/facebook.php');

    $config = array(
    'appId' => 'XXXXXXXXXXXXXXXXX',
    'secret' => 'XXXXXXXXXXXXXXXXX',
    );

    $facebook = new Facebook($config);
    $user_id = $facebook->getUser();

    if($user_id) {

    try {

    $user_profile = $facebook->api('/me','GET');

This post has been edited by mattrmclaren: 19 December 2011 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,800
  • Joined: 08-August 08

Re: Looping through a multidimensional array

Posted 19 December 2011 - 02:56 PM

So what's in facebook.php?
Was This Post Helpful? 0
  • +
  • -

#13 mattrmclaren   User is offline

  • D.I.C Head

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

Re: Looping through a multidimensional array

Posted 19 December 2011 - 03:03 PM

<?php
/**
 * Copyright 2011 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

require_once "base_facebook.php";

/**
 * Extends the BaseFacebook class with the intent of using
 * PHP sessions to store user ids and access tokens.
 */
class Facebook extends BaseFacebook
{
  /**
   * Identical to the parent constructor, except that
   * we start a PHP session to store the user ID and
   * access token if during the course of execution
   * we discover them.
   *
   * @param Array $config the application configuration.
   * @see BaseFacebook::__construct in facebook.php
   */
  public function __construct($config) {
    if (!session_id()) {
      session_start();
    }
    parent::__construct($config);
  }

  protected static $kSupportedKeys =
    array('state', 'code', 'access_token', 'user_id');

  /**
   * Provides the implementations of the inherited abstract
   * methods.  The implementation uses PHP sessions to maintain
   * a store for authorization codes, user ids, CSRF states, and
   * access tokens.
   */
  protected function setPersistentData($key, $value) {
    if (!in_array($key, self::$kSupportedKeys)) {
      self::errorLog('Unsupported key passed to setPersistentData.');
      return;
    }

    $session_var_name = $this->constructSessionVariableName($key);
    $_SESSION[$session_var_name] = $value;
  }

  protected function getPersistentData($key, $default = false) {
    if (!in_array($key, self::$kSupportedKeys)) {
      self::errorLog('Unsupported key passed to getPersistentData.');
      return $default;
    }

    $session_var_name = $this->constructSessionVariableName($key);
    return isset($_SESSION[$session_var_name]) ?
      $_SESSION[$session_var_name] : $default;
  }

  protected function clearPersistentData($key) {
    if (!in_array($key, self::$kSupportedKeys)) {
      self::errorLog('Unsupported key passed to clearPersistentData.');
      return;
    }

    $session_var_name = $this->constructSessionVariableName($key);
    unset($_SESSION[$session_var_name]);
  }

  protected function clearAllPersistentData() {
    foreach (self::$kSupportedKeys as $key) {
      $this->clearPersistentData($key);
    }
  }

  protected function constructSessionVariableName($key) {
    return implode('_', array('fb',
                              $this->getAppId(),
                              $key));
  }
}

Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,800
  • Joined: 08-August 08

Re: Looping through a multidimensional array

Posted 19 December 2011 - 03:16 PM

Ok, now base_facebook.php?
Was This Post Helpful? 0
  • +
  • -

#15 mattrmclaren   User is offline

  • D.I.C Head

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

Re: Looping through a multidimensional array

Posted 19 December 2011 - 03:57 PM

https://github.com/f...se_facebook.php
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2