Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:54:00+00:00 2026-05-27T14:54:00+00:00

I am attempting to use a recursive function to search through a multidimensional array,

  • 0

I am attempting to use a recursive function to search through a multidimensional array, such as the one below, to find certain values, i.e. people who went to certain school, majored in a certain subject, hold a certain job title, etc. In case your wondering, this array is output from the Facebook Graph API. In reality there are more than 3 offset arrays, depending on the number of friends a user has, it could be in the thousands.

Here’s a solution I tried with very little knowledge of recursive functions (my first thought was to use in_array before I found out it didn’t work for md arrays):

So to give you an idea of how the md array below works, check out this snippet of code:

$friend = $fqlResult[0]['name'];
echo "$friend";

*The output would be “BLANK” since I deleted the person’s name.

$data = $fqlResult;
$collegemajor = (isset($value['education'][0]['concentration'][0]['name'])) ? $value['education'][0]['concentration'][0]['name'] : null ;
$major = "Business Administration";
if (isset($collegemajor)) {
    foreach($data as $key=> $value) {
          if ($value($collegemajor) == $major) {
              echo "User $key is majoring in $major";
     }
  }
}

So here is the multidimensional array referenced above. In this example, I want to pull the names of all of the user’s friends who majored in Business Admin. in college. As you can see from this snippet, there aren’t any (I think) but in the long version of the array, there are plenty. The code above produces no output and I’m lost as to how to make it work. Any help would be greatly appreciated.

 Array
(
[0] => Array
    (
        [name] => BLANK
        [education] => 
        [work] => 
    )

[1] => Array
    (
        [name] => BLANK
        [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
                    )

            )

        [work] => Array
            (
            )

    )

[2] => Array
    (
        [name] => BLANK
        [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
                    )

            )

        [work] => 
    )

[4] => Array
    (
        [uid] => 1234567
        [name] => BOB NO ONE
        [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
                                    )

                            )

                    )

            )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:54:01+00:00Added an answer on May 27, 2026 at 2:54 pm

    There’s really no need for recursion for something like this, considering the depth of the tree is always fixed and the structure is known. Using some nested loops would do the trick:

    $friends = $fqlResult;
    $friends_BA = array();
    
    foreach ($friends as $friend) {
        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;
                            continue 3; // skip to the next friend
                        }
                    }
                }
            }
        }
    }
    
    var_dump($friends_BA);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use a similar Category implementation to this one in the Django
While attempting to use the CopyFile() function I have encountered a strange error. It
In attempting to use ed to delete lines around a certain pattern I've been
I'm attempting to use the adb pull command to copy only certain files (jpg)
I'm attempting to use a sample function on the PHP manual website (posted by
I'm attempting to use wget to recursively grab only the .jpg files from a
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng
In attempting to use the Performance Tools on an ASP.NET website I'm getting various
When attempting to use HttpWebRequest to retrieve a page from my dev server, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.