Walter Schrabmair
Walter Schrabmair

Reputation: 1341

how to get all keys of this multidimensional array

I have similar problem, which is here: solved Problem

But I would like to get such an array in return as final result for this demo array in 1.

thanks a lot!

return array I would like to get:

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

I have tried this: But does not work:

        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;            
        }            

Moreover I have found this:Maybe it could help to solve my request: get all path of an array

Upvotes: 0

Views: 925

Answers (2)

Walter Schrabmair
Walter Schrabmair

Reputation: 1341

Yes this link did what I wanted: 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;                                                
             }                                                                 

to call it by:

     print_r(getKeyPaths($jsonquotesum));

Upvotes: 2

Jilu
Jilu

Reputation: 197

Use array_key(). You will get all the keys. doc here:

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

Upvotes: 0

Related Questions