John Davidson

php - Issue when using array_merge to merge multiple arrays into a single one

0 comments
Message:


Before I jump into the code let me display the current representation of the array hierarchy


Array
(
[Details] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[itemId] => 9999
[itemName] => test
[itemPrice] => 0.00
)

)

[1] => Array
(
[0] => stdClass Object
(
[itemId] => 10
[itemName] => yest
[itemPrice] => 12
)

)

)
)

My desired outcome is slightly different


Array
(
[Details] => Array
(

[0] => stdClass Object
(
[itemId] => 9999
[itemName] => test
[itemPrice] => 0.00
)




[1] => stdClass Object
(
[itemId] => 10
[itemName] => yest
[itemPrice] => 12
)

)

)
)

To my understanding it looks like the problem is because my code generate a new array inside of each for eachloop. I tried using Array_merge to try and have them in a single array yet it turned out that it only returned a single stack of elements.


Would really appreciate if someone can help me identify my mistake.


My code


public function getData($list)
{

$itemDetails= array();

foreach ($listas $Item) {

$query = $this->db->get_where('items_table', array('Name' => $Item));

$queryResult = $query->result();
$itemDetails[] = $queryResult;

$result = array_merge($itemDetails,$queryResult);

//$temp = array_merge($toppingDetails,$queryResult);




}

return array('Details'=>$itemDetails);
}


My Answer:
Array
(
[Details] => Array
(
[0] => Array
(
[name] => John
[age] => 25
)

[1] => Array
(
[name] => Jane
[age] => 30
)

)

)
Now, let's say we have two more arrays that we want to merge with the existing "Details" array. Here are the two arrays:

Array
(
[Details] => Array
(
[2] => Array
(
[name] => Alice
[age] => 28
)

)

)

Array
(
[Details] => Array
(
[3] => Array
(
[name] => Bob
[age] => 35
)

)

)
If we try to merge these arrays using array_merge, we will encounter an issue because array_merge will overwrite the existing "Details" array instead of merging the new arrays into it. Here is the code that demonstrates this issue:

php
$existingArray = [
'Details' => [
['name' => 'John', 'age' => 25],
['name' => 'Jane', 'age' => 30]
]
];

$newArray1 = [
'Details' => [
['name' => 'Alice', 'age' => 28]
]
];

$newArray2 = [
'Details' => [
['name' => 'Bob', 'age' => 35]
]
];

$mergedArray = array_merge($existingArray, $newArray1, $newArray2);

print_r($mergedArray);


The output of this code will be:


Array
(
[Details] => Array
(
[0] => Array
(
[name] => Bob
[age] => 35
)

)

)
As you can see, only the last array (newArray2) is merged into the existing "Details" array, and the previous arrays are overwritten.

To solve this issue, we can use array_merge_recursive instead of array_merge. Here is the updated code:

php
$mergedArray = array_merge_recursive($existingArray, $newArray1, $newArray2);

print_r($mergedArray);


Now, the output will be:


Array
(
[Details] => Array
(
[0] => Array
(
[name] => John
[age] => 25
)

[1] => Array
(
[name] => Jane
[age] => 30
)

[2] => Array
(
[name] => Alice
[age] => 28
)

[3] => Array
(
[name] => Bob
[age] => 35
)

)

)
Now, all the arrays are merged into the existing "Details" array without overwriting any data.

Rate this post

3 of 5 based on 7122 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us