代码一:
$file = 'pandawill_categories.txt';
$content = file_get_contents($file);$arraystate=json_decode($content, true);var_export($arraystate);运行结果得到一个多维数组,由于输出比较长,我粘贴出一部分如下:
array (
  0 => 
  array (
    'id' => '392',
    'text' => 'PandaWill@Home',
    'checked' => false,
    'children' => 
    array (
      0 => 
      array (
        'id' => '393',
        'text' => 'PandaWill in US',
        'checked' => false,
        'children' => 
        array (
          0 => 
          array (
            'id' => '394',
            'text' => 'Apple Accessories',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
          1 => 
          array (
            'id' => '395',
            'text' => 'Electronics',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
          2 => 
          array (
            'id' => '462',
            'text' => 'Toys',
            'checked' => false,
            'children' => 
            array (
              0 => 
              array (
                'id' => '463',
                'text' => 'Car Model',
                'checked' => false,
                'children' => 
                array (
                ),
              ),
              1 => 
              array (
                'id' => '464',
                'text' => 'R/C Toys',
                'checked' => false,
                'children' => 
                array (
                ),
              ),
              2 => 
              array (
                'id' => '396',
                'text' => 'Others',
                'checked' => false,
                'children' => 
                array (
                ),
              ),
            ),
          ),
          3 => 
          array (
            'id' => '465',
            'text' => 'Bath',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
          4 => 
          array (
            'id' => '467',
            'text' => 'Home & Garden',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
          5 => 
          array (
            'id' => '466',
            'text' => 'Others',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
        ),
      ),
      1 => 
      array (
        'id' => '397',
        'text' => 'PandaWill in UK',
        'checked' => false,
        'children' => 
        array (
          0 => 
          array (
            'id' => '398',
            'text' => 'Apple Accessories',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
          1 => 
          array (
            'id' => '399',
            'text' => 'Home & Garden',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
          2 => 
          array (
            'id' => '481',
            'text' => 'Bath',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
          3 => 
          array (
            'id' => '400',
            'text' => 'Others',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
        ),
      ),
      2 => 
      array (
        'id' => '401',
        'text' => 'PandaWill in AU',
        'checked' => false,
        'children' => 
        array (
          0 => 
          array (
            'id' => '402',
            'text' => 'Apple Accessories',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
          1 => 
          array (
            'id' => '403',
            'text' => 'Home & Garden',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
          2 => 
          array (
            'id' => '404',
            'text' => 'Others',
            'checked' => false,
            'children' => 
            array (
            ),
          ),
        ),
      ),
    ),
  ),
  1 => 
  array (
    'id' => '18',
    'text' => 'Apple Accessories',
    'checked' => false,
    'children' => 
    array (
      0 => 
      array (
        'id' => '248',
        'text' => 'Ipad Keyboard & Iphone Keyboard',
        'checked' => false,
        'children' => 
        array (
        ),
      ),
      1 => 
      array (
        'id' => '249',
        'text' => 'iPod Touch',
        'checked' => false,
        'children' => 
        array (
        ),
      ),
      2 => 
      array (
        'id' => '250',
        'text' => 'iPod Shuffle',
        'checked' => false,
        'children' => 
        array (
        ),
      ),
      3 => 
      array (
        'id' => '251',
        'text' => 'iPod Nano',
        'checked' => false,
        'children' => 
        array (
        ),
      ),
      4 => 
      array (
        'id' => '252',
        'text' => 'iPod Classic',
        'checked' => false,
        'children' => 
        array (
        ),
      ),
      5 => 我想从上面的结果中,得到id,text,因为是多维,想做成一个多级下拉的菜单,就是一维就作为一级目录,然后,对应id下面的就是二级,三级,四级目录,显示三一级目录后面,如这个
 0 => 
  array (
    'id' => '392',
    'text' => 'PandaWill@Home',
    'checked' => false,
    'children' => 
    array (
      0 => 
      array (
        'id' => '393',
        'text' => 'PandaWill in US',
        'checked' => false,
        'children' => 
        array (
          0 => 
          array (
            'id' => '394',
            'text' => 'Apple Accessories',
            'checked' => false,
            'children' => 
            array (
            ),
          )一级id是392,一级目录text是PandaWill@Home;然后一级id是392下面children又是一个数组,可能有多个值,全部取出来,id和text就作为一级392下面的二级目录,以此类推,
想要的最终效果是:一个多级下拉菜单, 鼠标移到一级目录,后面有子目录就显示出来,我的目的是,通过选择这个目录,然后就通过这个id,去数据库查出属于这一级的所有产品,

解决方案 »

  1.   

    唉,你var_export下嘛,这样大家也好直接拿来使用呀,。你要做类似tree view效果?
      

  2.   


    var_export,得到的就是上面的数组,已经贴出来了,认真看我的问题,谢谢
      

  3.   

    如果用ajax来获取子菜单的数据,就方便多了。如果要一次预读数组数据,如果栏目层次不多可以手动构建。如果栏目层次较多,你还需要写一个tree进行构建。直到数据合理