Home > Software engineering >  How to implode multi tree array to string
How to implode multi tree array to string

Time:12-09

i am trying implode this type of array to string. how can i do this ?

Array ( [58] => Array ( [272] => Array ( ) [273] => Array ( ) [274] => Array ( ) [275] => Array ( ) [276] => Array ( ) [507] => Array ( ) ) [182] => Array ( [46] => Array ( ) [96] => Array ( ) ) [271] => Array ( [2] => Array ( ) [75] => Array ( ) [80] => Array ( ) ) [277] => Array ( ) [278] => Array ( [4] => Array ( [174] => Array ( ) ) [14] => Array ( ) [36] => Array ( ) [57] => Array ( ) [279] => Array ( ) [280] => Array ( ) [281] => Array ( ) ) )

i want to get result like: 58, 272, 273, 274, 275, 276, 507, 182 ...

CodePudding user response:

I must say your array is pretty strange but here is what i got from your code and request :

$thing = Array ( 
        58 => Array ( 272 => Array ( ), 273 => Array ( ), 274 => Array ( ), 275 => Array ( ), 276 => Array ( ), 507 => Array ( ) ),
        182 => Array ( 46 => Array ( ), 96 => Array ( ) ),
        271 => Array ( 2 => Array ( ), 75 => Array ( ), 80 => Array ( ) ),
        277 => Array ( ),
        278 => Array ( 4 => Array ( 174 => Array ( ) ), 14 => Array ( ), 36 => Array ( ), 57 => Array ( ), 279 => Array ( ), 280 => Array ( ), 281 => Array ( ) ) 
      );
$temp=[];
function husk_that_thing($array,&$temp)
{
  foreach ($array as $key => $value) 
  {
    $temp[]=$key;
    if($value)
    {
      husk_that_thing($value,$temp);
    }
  }
}
husk_that_thing($thing,$temp);
echo implode(',',$temp);//output

CodePudding user response:

Did you try implode()? See this

  • Related