Reputation: 1417
I need to restructure an array so that indexed rows of data are pushed into a new associative element called records
.
Input array:
array (
'code' => 200,
'message' => 'OK',
0 =>
array (
'title' => 'Green peppercorn and lemongrass coconut broth',
'media' => '/posts/images/84709.jpg',
),
1 =>
array (
'title' => 'Green peppercorn and lemongrass coconut broth',
'media' => '/posts/images/84709.jpg',
),
)
Desired result:
array (
'code' => 200,
'message' => 'OK',
'records' => [
array (
'title' => 'Green peppercorn and lemongrass coconut broth',
'media' => '/posts/images/84709.jpg',
),
array (
'title' => 'Green peppercorn and lemongrass coconut broth',
'media' => '/posts/images/84709.jpg',
),
),
)
It used to be two arrays; I merged with array_merge($message, $records);
Upvotes: 0
Views: 49
Reputation: 47874
You don't need to loop over the whole payload.
If you know that there are two elements at the start of the array to ignore (and it looks like a predictable format to me), just splice the remaining elements from the array, then re-append the data to the original array under the key records
. Demo
$array['records'] = array_splice($array, 2);
But actually, if you are saying
It used to be two arrays; I merged with
array_merge($message, $records);
And your input arrays are:
$message = ['code' => 200, 'message' => 'OK'];
$records = [
[
'title' => 'Green peppercorn and lemongrass coconut broth',
'media' => '/posts/images/84709.jpg',
],
[
'title' => 'Green peppercorn and lemongrass coconut broth',
'media' => '/posts/images/84709.jpg',
],
];
Then all you need to do is declare a new element containing the records
payload. Demo
$message['records'] = $records;
Or use the union operator (or union assignment operator) with a temporary associative array: Demo
$message += ['records' => $records];
or Demo
$message += compact('records');
Upvotes: 0
Reputation: 83
If you want a short code (one string , two with $result declaration)
$json = '{"code":200,"message":"OK","0":{"title":"Green peppercorn and lemongrass coconut broth","media":"\/posts\/images\/84709.jpg"},"1":{"title":"Green peppercorn and lemongrass coconut broth","media":"\/posts\/images\/84709.jpg"}}';
$result = [];
foreach(json_decode($json,true) as $k=>$v) if(is_array($v)){$result["records"][]=$v;} else {$result[$k]=$v;};
Make sure to change $json with your json
result (pretty_printed):
{
"code": 200,
"message": "OK",
"records": [
{
"title": "Green peppercorn and lemongrass coconut broth",
"media": "\/posts\/images\/84709.jpg"
},
{
"title": "Green peppercorn and lemongrass coconut broth",
"media": "\/posts\/images\/84709.jpg"
}
]
}
Upvotes: 1
Reputation: 16117
If you want to continue with your json
response then you can create a new array as like, but this example is only work for your json
which you mentioned in your question:
<?php
$array = json_decode('{"code":200,"message":"OK","0":{"title":"Green peppercorn and lemongrass coconut broth","media":"\/posts\/images\/84709.jpg"},"1":{"title":"Green peppercorn and lemongrass coconut broth","media":"\/posts\/images\/84709.jpg"}}
',true);
$newArray = array(); // initialize new array
foreach ($array as $key => $value) {
if(is_array($value)) { // if having array
$newArray['records'][] = $value;
}
else{
$newArray[$key] = $value;
}
}
echo json_encode($newArray);
?>
Result:
{"code":200,"message":"OK","records":[{"title":"Green peppercorn and lemongrass coconut broth","media":"\/posts\/images\/84709.jpg"},{"title":"Green peppercorn and lemongrass coconut broth","media":"\/posts\/images\/84709.jpg"}]} Second, if you are mergin two array `array_merge($message, $records);`
Second Solution (recommended), if you are combining two array and wants to add a new index records
then you can also modify by adding a records
index as:
$newArray = $message;
$newArray['records'] = $records;
echo json_encode($newArray);
Upvotes: 1