Php 将阵列合并为一个组

Php 将阵列合并为一个组,php,arrays,Php,Arrays,对不起,这个问题可能很愚蠢,但我真的需要你的帮助。我得到了数组: {"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\/8470

对不起,这个问题可能很愚蠢,但我真的需要你的帮助。我得到了数组:

{"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"}}
我们需要做到这一点:

{"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"}]}
请让我知道如何使用PHP…它曾经是我与
array\u merge($message,$records)合并的两个数组


谢谢你

如果你想继续你的
json
响应,那么你可以像这样创建一个新数组,但是这个例子只适用于你在问题中提到的
json

<?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);
?>
第二种解决方案(推荐),如果您要组合两个数组并希望添加新索引
记录
,则还可以通过添加
记录
索引进行修改,如下所示:

$newArray = $message;
$newArray['records'] = $records;
echo json_encode($newArray);

如果您想继续您的
json
响应,那么您可以创建一个新数组,如下所示,但此示例仅适用于您在问题中提到的
json

<?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);
?>
第二种解决方案(推荐),如果您要组合两个数组并希望添加新索引
记录
,则还可以通过添加
记录
索引进行修改,如下所示:

$newArray = $message;
$newArray['records'] = $records;
echo json_encode($newArray);
如果需要短代码(一个字符串,两个带$result声明)

$json='{“code”:200,“message”:“OK”,“0”:{“title”:“绿胡椒和柠檬草椰子汤”,“media”:“\/posts\/images\/84709.jpg”},“1”:{“title”:“绿胡椒和柠檬草椰子汤”,“media”:“\/posts\/images\/84709.jpg”}”;
$result=[];
foreach(json_decode($json,true)为$k=>$v)if(is_array($v)){$result[“records”][]=$v;}否则{$result[$k]=$v;};
确保用json更改$json

结果(打印精美):

如果需要短代码(一个字符串,两个带$result声明)

$json='{“code”:200,“message”:“OK”,“0”:{“title”:“绿胡椒和柠檬草椰子汤”,“media”:“\/posts\/images\/84709.jpg”},“1”:{“title”:“绿胡椒和柠檬草椰子汤”,“media”:“\/posts\/images\/84709.jpg”}”;
$result=[];
foreach(json_decode($json,true)为$k=>$v)if(is_array($v)){$result[“records”][]=$v;}否则{$result[$k]=$v;};
确保用json更改$json

结果(打印精美):

怎么样
$message[“records”]=$records
?怎么样
$message[“records”]=$records