best solution to get values of multidimensional array in php -
i trying values of multidimensional array this
$post = array( 'k'=>'kk', 'l'=>'ll', 'n'=>array( 't'=>'tt', 'n'=>array( 'j'=>'jj', 'h'=>'hh' ) ) );
my approach :
$ordered = array(); foreach($post $key=>$value){ if(!is_array($value)){ $ordered[] = $value; }else{ $r = array_walk_recursive($value, function($v,$k) use(&$ordered){ $ordered[] =$v; }); } }
expected output:
array ( [0] => kk [1] => ll [2] => tt [3] => jj [4] => hh )
i don't know if it's best solution , consider performance , backward compatibility older php
$ordered = array(); function myfunction($value, $key){ $ordered[] = $value; } array_walk_recursive($post ,"myfunction") var_dump($ordered);
try one. may work condition.
edit
$ordered = array(); function myfunction($value, $key){ global $orderd; $ordered[] = $value; } array_walk_recursive($post ,"myfunction") var_dump($ordered);
this work correctly
Comments
Post a Comment