我使用以下代碼循環遍歷多維數組,並找到去某個大學的用戶,在本例中爲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);
多維數組看起來像這樣。這是從Facebook的圖形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
)
)
)
)
)
我碰到了類似的程序,找出用戶的主修是做了工作的某些科目,但由於某些原因,上面的代碼不起作用。這是工作的那個:
$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);
一如既往,任何幫助將真誠讚賞。節日快樂!
UPDATE
任何人想利用這一個刺?
$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);
數組是這樣的:
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
)
)
被玩弄了一個小時,但似乎無法使它發揮作用。我在第二個foreach聲明中得到了無效的論點。
測試的代碼,完全按照與陣列結構和3項。它返回了預期的結果。你確定你正在使用最新版本嗎?你確定你的'd'功能正確嗎? – 2011-12-19 20:31:23
你指的是第一個正確的?尋找UCF學生的那個? – buttonitup 2011-12-19 20:36:28
大聲笑哎呀:)我的歉意,讓我看看現在的頂端。請參閱下面的解答。 – 2011-12-19 20:38:01