如何获取这个多维数组的所有键

时间:2017-09-13 12:00:18

标签: php multidimensional-array

我有类似的问题,就在这里:solved Problem

但是我希望得到这样一个数组作为1中此演示数组的最终结果。

非常感谢!

返回数组我想得到:

    [0] => "0.name "
    [1] => "0.id "
    [2] => "0.phone "
    [3] => "0.Base.city "
    [4] => "0.EBase.city "
    [5] => "0.Qty "
    [6] => "1.name "
    [7] => "1.id "
    [8] => "1.phone "
    [9] => "1.Base.city "
    [10]=> "1.EBase.city "
    [11]=> "1.Qty "

我试过这个:但是不起作用:

        function array_flat(array $myarr)            
        {            
            $myline = "";            
            foreach ($myarr as $key => $value)            
                {            
                if ( $myline == "" ){$myline=sprintf("%s",$myarr[$key]);}            
                else {$myline=sprintf("%s%s",$myline,$myarr[$key]);}            
                }            
            return array($myline);            
        }            

        function array_keys_multi(array $array)            
        {            
            $keys = array();            

            foreach ($array as $key => $value) {            
            $keys[] = $key;            

            if (is_array($value)) {            
                $keys = array_merge($keys, array_keys_multi($value));            
            }            
                else {            
                    $keys = array_flat($keys);            
                }            

            }            

            return $keys;            
        }            

此外我发现了这个:也许它可以帮助我解决我的要求: get all path of an array

2 个答案:

答案 0 :(得分:2)

是的,这个链接做了我想要的: Solution

             function getKeyPaths(array $tree, $glue = '_')                    
             {                                                                 
                 $paths = array();                                             
                 foreach ($tree as $key => &$mixed) {                          
                     if (is_array($mixed)) {                                   
                         $results = getKeyPaths($mixed, $glue);                
                         foreach ($results as $k => &$v) {                     
                             $paths[$key . $glue . $k] = $v;                   
                         }                                                     
                         unset($results);                                      
                     } else {                                                  
                         $paths[$key] = $mixed;                                
                     }                                                         
                 }                                                             

                 return $paths;                                                
             }                                                                 

通过以下方式调用它:

     print_r(getKeyPaths($jsonquotesum));

答案 1 :(得分:0)

使用array_key()。 你会得到所有的钥匙。 doc在这里:

http://php.net/manual/fr/function.array-keys.php