如何获取此数组的索引

时间:2016-08-05 11:02:55

标签: codeigniter

{{1}}

1 个答案:

答案 0 :(得分:0)

让我们用递归:

来做
  public function array_print(){


        $myArray = array(
                    'type'=>'Array outside 1',
                'name'=>'Array outside 2',
                array(
                    'file_name'=>'second array',
                    array(
                            'file_ext'=>'third array',
                        array('text'=>'Fourth array inside thrid array')
                            )
                        )           

                    );

        Print "<pre>";

        $output=array();
        $result=$this->recursive($myArray);    
        print_r($result);
        //Output value has key and value. I used ## as separator
        foreach ($result as $value) {
            $data = explode('##',$value);
            $output[$data[0]] = $data[1];
        }
        print_r($output);


  }


    function recursive($array){
        global $temp_data;

        if(!empty($array)){
        foreach($array as $key => $value){
         //If $value is an array.
            if(is_array($value)){
                //We need to loop through it.
                return $this->recursive($value);
            } else{                  
                   $temp_data[]= $key.'##'.$value;
                }
            }
        }
        return $temp_data;
    }

使用它我们可以循环遍历多维数组并将其存储在Global数组中。 $ myArray是你的数组。