i want to add multiple Array item into One Array Object in PHP
i have array like below :
[
{
"name": "AAA"
},
{
"family": "BBB"
},
{
"job": "CCC"
}
]
And i need to Convert like below:
{
"name": "AAA",
"family": "BBB",
"job": "CCC"
}
Array Data maybe changed , but , i write this code for explain my problem :
$RetArray=array();
$Array_Test=array(array('name'=>'AAA'),array('family'=>'BBB'),array('job'=>'CCC'));
foreach ($Array_Test as $json_item){
foreach ($json_item as $key=>$value){
array_push($RetArray,array($key => $value));
}
}
echo json_encode($RetArray);
But this code returns the same as the first array!
I want to return every item into one array.
My Answer:
You can achieve this by using the array_merge function in PHP. Here is an example of how you can insert multiple array items into one array object:
php
// Array items to be inserted
$array1 = array('item1', 'item2');
$array2 = array('item3', 'item4');
// Create an empty array object
$mergedArray = array();
// Insert multiple array items into the merged array object
$mergedArray = array_merge($mergedArray, $array1, $array2);
// Print the merged array object
print_r($mergedArray);
This will output:
Array
(
[0] => item1
[1] => item2
[2] => item3
[3] => item4
)
Rate this post
4 of 5 based on 1028 votesComments