Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Array Merge - Removing Array Keys from an Array before Merging

Tags:

php

Below is the print_r($result) result of an $result = array_merge(array($id => $value), array($user => $value), array("Information" => $value)) variable in a piece of PHP code:

Array ( [id] => 1 [user] => 1 
    [Information] => Array ( [0] => Array ( [name] => 'John' ) 
                            [1] => Array ( [family] => 'Goldenberg' ) 
                            [2] => Array ( [age] => '21' )))

How to remove the array keys from the "Information" => $value array in PHP to make the output like below:

Array ( [id] => 1 [user] => 1 
    [Information] => Array ([name] => 'John' 
                            [family] => 'Goldenberg' 
                            [age] => '21'))

Is there any specific function in PHP to complete this task? Thanks a lot for your help.

like image 972
Sami Avatar asked Jun 17 '26 08:06

Sami


1 Answers

Your "Information" array is a multidimensional one, having some arrays as the elements, in which there are some "key-value" pairs as "data". You may use the following to reinsert the "data" in the desirable way:

<?php
$info = array( array('name'=>'John'), array('family' => 'Goldenberg'), array('age' => 21));
$out = array();
foreach($info as $arr)
    foreach($arr as $key => $val)
        $out[$key] = $val;
print_r($out);
?>
like image 68
someOne Avatar answered Jun 19 '26 21:06

someOne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!