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!
UPDATE
Anyone want to take a stab at this one?
$friends = $fqlResult;
$friends_BA = array();
foreach ($friends as $friend) {
$isBA = false;
if (is_array($friend['current_location'])) {
foreach ($friend['current_location'] as $location) {
if (isset($location)) {
foreach ($location['city'] as $city) {
$lowerName = strtolower($city);
if (strpos($lowerName, 'orlando') !== false || strpos($lowerName, 'gainesville') !== false) {
$friends_BA[] = $friend['name'];
continue 3; // skip to the next friend
}
}
}
}
}
}
d($friends_BA);
The array looks like this:
Array
(
[0] => Array
(
[name] => PERSONS NAME
[current_location] => Array
(
[city] => New York
[state] => New York
[country] => United States
[zip] =>
[id] => 108424279189115
[name] => New York, New York
)
)
[1] => Array
(
[name] => PERSONS NAME
[current_location] =>
)
[2] => Array
(
[name] => PERSONS NAME
[current_location] =>
)
[3] => Array
(
[name] => PERSONS NAME
[current_location] =>
)
[4] => Array
(
[name] => PERSONS NAME
[current_location] => Array
(
[city] => San Jose
[state] => California
[country] => United States
[zip] =>
[id] => 111948542155151
[name] => San Jose, California
)
)
[5] => Array
(
[name] => PERSONS NAME
[current_location] => Array
(
[city] => Boston
[state] => Massachusetts
[country] => United States
[zip] =>
[id] => 106003956105810
[name] => Boston, Massachusetts
)
)
Been playing around with it for an hour but can’t seem to make it work. I’m getting invalid arguments on the second foreach statement.
Your problem is case SensiTivity. You use
strtoloweron a string to check the university, however, the university string you posted has mixed case. Change it to this line and it should work:If you want the cases to match, remove the
strtolower, add the uppercase letters back in and it should match case as well.UPDATE
The second part of the problem is
$name['name']should be just$name. Since you are accessing the school array directly, it is looping through that portion.That should do it. So basically the foreach was looping over the school array, so the name comes out as a string value instead of being apart of the array.
UPDATE 2