PHP配列再帰メソッドの複数のインスタンス

WBOY
リリース: 2016-07-25 09:11:57
オリジナル
1124 人が閲覧しました

php数组递归方法

有如下php数组:

  1. function genTree5($items) {
  2. foreach ($items として $item)
  3. $items[$item['pid']]['son'][$item['id ']] = &$items[$item['id']];
  4. isset($items[0]['son']) を返す ? $items[0]['son'] : array();
  5. }
复制代

方法二:

  1. function findChild($arr,$id){

  2. $childs=array();
  3. foreach ($arr as $k => $v){
  4. if($v['pid']== $id){
  5. $childs[]=$v;
  6. }
  7. }
  8. // echo "
    ";print_r($childs);die(); 
  9. $childs を返します;
  10. }

  11. function build_tree($root_id){

  12. global $items;
  13. $childs =array();
  14. $childs=findChild($items,$root_id);
  15. // print_r($childs);
  16. // 死ぬ();
  17. if(empty($childs)){
  18. null を返します。
  19. }
  20. foreach ($childs as $k => $v){
  21. $rescurTree=build_tree($v['id']);
  22. if( null != $rescurTree){
  23. $childs[$k]['son']=$rescurTree;
  24. }
  25. }
  26. $childs を返します。
  27. }

复制代


ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!