2

let's say if I have an array like this below.

$i = array(
    'arr1'  => array(
        'a11'   => 'ar11',
        'a12'   => 'ar12',
    ),
    'arr2'  => array(
        'a21'   => 'ar21',
        'a22'   => 'ar22',
        'a23'   => 'ar23',
    ),
    'arr3'  => 'arrr3',
    'arr4'  => 'arrr4'
);

I want a new array that created from this array like the one I mentioned below,

$j = array(
    'a11'   => 'ar11',
    'a12'   => 'ar12',
    'a21'   => 'ar21',
    'a22'   => 'ar22',
    'a23'   => 'ar23',
    'arr3'  => 'arrr3',
    'arr4'  => 'arrr4'
);

sofar I tried extracting the last two elements which aren't arrays I mention the code below

$ii = $i;
foreach($ii as $k => $v):
    if(is_array($v)):
        unset($ii[$k]);
    endif;
endforeach;

this returns me the last two elements. but the way i tried extracting the other elements looks wired see below.

$i1 = $i['arr1'];
$i2 = $i['arr2'];
$i3 = array_merge($i1, $i2);
$final = array_merge($i3, $j);

this looks simple because it has small amout of elemetns but i have large amount of elements in my project, any other ways to get this output?

2 Answers2

1

Can you try this?

$final = [];

foreach($i as $key => $value){
    if(is_array($value){
        $final = array_merge($final, $value);

    }   else{
        $final[$key] = $value;
    }
}

Upd.

@Magnus-Eriksson insists to explain what is going on within loop. At first we check if value is an array or not. If value is an array we merge nested array with (new, created before loop starts) array where we putting our elements. If value isn't array we just get that value and add it to the final array. There is no magic here. But answer with array_walk_recursive() below is much more elegant but with "magic" within.

webprogrammer
  • 2,393
  • 3
  • 21
  • 27
  • Instead of just posting a code snippet, tailored to the OP's issue, you should also include a proper explanation of what it solves and how. If not, the question won't be particularly helpful for future visitors. – M. Eriksson Feb 15 '20 at 10:23
  • Waiting for the author to comment what? Your code either solves the OP's issue or it doesn't. They have shown us the expected result so you should know if it does. – M. Eriksson Feb 15 '20 at 10:33
1

array_walk_recursive() is what you need:

$a = array(
    'arr1'=> array(
        'a11' => 'ar11',
        'a12' => 'ar12',
    ),
    'arr2'=> array(
        'a21' => 'ar21',
        'a22' => 'ar22',
        'a23' => 'ar23',
    ),
    'arr3' => 'arrr3',
    'arr4' => 'arrr4'
);

$b = array();
array_walk_recursive($a, function($val, $key) use(&$b)
{
    $b[$key] = $val;
});

You can test it here.

Olivier
  • 13,283
  • 1
  • 8
  • 24
  • it works, but I don't know what's happening inside your code even after I read the docs about array_walk_recurssive function. – Vigneshwaran Feb 15 '20 at 10:38
  • Basically array_walk_recursive() visits all the elements contained in $a that are simple values (i.e. not arrays). – Olivier Feb 15 '20 at 10:41
  • hmm, but how did you apply the condition? what is that use(&$b) means? – Vigneshwaran Feb 15 '20 at 10:42
  • The `use` keyword allows a closure to capture a variable. See [this](https://www.php.net/manual/en/functions.anonymous.php) and [that](https://stackoverflow.com/questions/1065188/in-php-what-is-a-closure-and-why-does-it-use-the-use-identifier) for more informations. – Olivier Feb 15 '20 at 10:48