5

i have array look like this:

$arr=array(
   'key1'=>'value1',
   'key2'=>'value2',
   0=>array(
      'sub1_key1'=>'sub1_value1',
      'sub1_key2'=>'sub1_value2',
   ),
   'key3'=>array(
      'sub2_key1'=>'sub2_value1',
      'sub2_key2'=>'sub2_value2',
   ),
   //....
);

how to converter array $arr to array look like this:

$arr=array(
   'key1'=>'value1',
   'key2'=>'value2',
   'sub1_key1'=>'sub1_value1',
   'sub1_key2'=>'sub1_value2',
   'sub2_key1'=>'sub2_value1',
   'sub2_key2'=>'sub2_value2',
   //....
);

somebody can help me?

4 Answers 4

4

Try this out. This works for you hopefully.

This is not trivial, because you are going to reduce multi.dim. array to single one and also ignore the key of the first array.
I have tested the following code and it produces that result you have shown.

function ChangeArrayToSingleArray($array) {  
   if (!$array) return false; 
   $flat = array(); 
   $iterator  = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); 

   foreach ($iterator as $key=>$value)
     $flat[$key] = $value; 
     return $flat; 
} 

var_dump(ChangeArrayToSingleArray($arr));

output is some thing like.

array (size=6)
  'key1' => string 'value1' (length=6)
  'key2' => string 'value2' (length=6)
  'sub1_key1' => string 'sub1_value1' (length=11)
  'sub1_key2' => string 'sub1_value2' (length=11)
  'sub2_key1' => string 'sub2_value1' (length=11)
  'sub2_key2' => string 'sub2_value2' (length=11)
Sign up to request clarification or add additional context in comments.

2 Comments

Good answer too, but I think the previous one is more easier to be understood. At least for me.
It uses Construct a RecursiveIteratorIterator php.net/manual/en/recursiveiteratoriterator.construct.php and RecursiveArrayIterator php.net/manual/en/class.recursivearrayiterator.php
4
$new = array();

foreach ($arr as $key => $value) {
    if (is_array($value)) {
        foreach ($value as $k => $v) { // having nested loop to save keys and values into new array
            $new[$k] = $v;
        }
    } else {
        $new[$key] = $value; // leaving values intact
    }
}

print_r($new); // returns  Array ( [key1] => value1 [key2] => value2 [sub1_key1] => sub1_value1 [sub1_key2] => sub1_value2 [sub2_key1] => sub2_value1 [sub2_key2] => sub2_value2 )

DEMO

Comments

1

Use array_walk () function that may help you https://www.php.net/array_walk

1 Comment

How to implement array_walk with this? Please, show us, it is possible.
1

you can try

function flatten($array) {
   $new_array = array();
   foreach ($array as $key => $value) {
      if ( is_array($value) ) {
         $new_array = array_merge ( $new_array, flatten($value));
      } else {
         $new_array[$key] = $value;
      }
   }
   return $new_array;
}

then you can just call :

$flattened_array = flatten($arr);

in your case

this will work with any depth array as it works recursively

here's the php fiddle demo

2 Comments

It is not saving key1 and key2 though !
According to your update your function only returns first to indices, aren't you testing your code ! ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.