# 多次元配列の再帰関数？

ここで私は再帰関数の知識はほとんど試してみました（私の最初の考えは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 ;
if (isset(\$collegemajor)) {
foreach(\$data as \$key=> \$value) {
if (\$value(\$collegemajor) == \$major) {
echo "User \$key is majoring in \$major";
}
}
}
``````

`````` 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
)

)

[type] => College
)

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

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

)

)

[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
)

[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
)

)

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

)

)

)
``````
0

## 2 答え

ツリーの深さは常に固定されており、構造が分かっていることを考えると、このような再帰の必要はありません。ネストされたループを使用すると、

``````\$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;
}
}
}
}
}
}

var_dump(\$friends_BA);
``````
0

いくつかのケースでは、` [education] `` null `であることに気付かなかった。私はちょうど答えを更新しました...そのバージョンを試してください。

（そして、` name `の代わりに` id `フィールドを検索するように更新しました）

` is_array `` isset `に変更すると、通知が取り除かれます。

どうもありがとう！私は終わったと思った。

join.meにコード895-165-764を入力してください

join.meセッションにあなたを招待して、私の画面を表示できますか？

うん、それをやることなく私たちが行ったことの通知。

``````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に設定するのではなく、パフォーマンスを向上させるために、関数の実行を停止し、配列の検索を続行しないようにすることができます。

それをそう呼んでください

``````foreach(\$fqlResult as \$result){