我试图使用递归函数来搜索多维数组,例如下面的数组,以找到某些值,即去某个学校,主修某个科目,持有某个职称等的人如果你想知道,这个数组是从Facebook Graph API输出的。实际上有超过3个偏移数组,取决于用户拥有的朋友数量,它可能有数千个。
这是我尝试的一个解决方案,对递归函数知之甚少(我的第一个想法是在我发现它不适用于md数组之前使用in_array):
为了让您了解下面的md数组是如何工作的,请查看以下代码片段:
$friend = $fqlResult[0]['name'];
echo "$friend";
*因为我删除了这个人的姓名,所以输出将是“BLANK”。
$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";
}
}
}
所以这是上面引用的多维数组。在这个例子中,我想提取在Business Admin中主要的所有用户朋友的名字。在大学。正如你从这个片段中看到的,没有(我认为),但在数组的长版本中,有很多。上面的代码没有输出,我迷失了如何使它工作。任何帮助将不胜感激。
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
)
)
)
)
答案 0 :(得分:0)
考虑到树的深度总是固定的并且结构是已知的,实际上不需要递归这样的东西。使用一些嵌套循环可以解决这个问题:
$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);
答案 1 :(得分:0)
您需要一个能够在数组中的特定字段上找到特定值的函数
function arraySearch($key, $value, $array){
$flag = FALSE;
foreach($array as $result){
if(arraySearch($key, $value, $result)){
$flag = TRUE
}elseif(isset($result[$key] && $result[$key] == $value){
$flag = TRUE;
}
}
return $flag
}
为了提高性能而不是将$ flag设置为true,您可以返回true,因为它将停止执行该函数并阻止它继续搜索数组。
这样称呼
foreach($fqlResult as $result){
if(arraySearch('concentration', 'Business Administration', $result)){
//You have found a user you are looking for, echo $result['name'] or do what you want with the result.
}
}