-1

I am trying to get all values of multidimensional array like this

$post = array(
   'k'=>'kk',
   'l'=>'ll',
   'n'=>array(
       't'=>'tt',
       'n'=>array(
           'j'=>'jj',
           'h'=>'hh'
       )
   )
);

My approach to do that is :

$ordered = array();
foreach($post as $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 the best solution for it , i am consider the performance and backward compatibility with older php

1 Answers1

0
$ordered = array();
function myfunction($value, $key){
   $ordered[] = $value;
}
array_walk_recursive($post ,"myfunction")
var_dump($ordered);

Try this one. It may be work for your condition.

EDIT

$ordered = array();
    function myfunction($value, $key){
       global $orderd;
       $ordered[] = $value;
    }
    array_walk_recursive($post ,"myfunction")
    var_dump($ordered);

This will work correctly

Vignesh Bala
  • 889
  • 6
  • 25
  • "This will work correctly" <- It won't work correctly while there is a variable typo. You did not test your code. – mickmackusa Feb 19 '22 at 21:15