我在json中有以下数组作为响应,我希望将其转换为单个数组。
{
"profileStatus": [
{
"Total percentage": "50.00",
"Fields blank": 4
}
],
"lname": [
{
"lastName": null
}
],
"photo": [
{
"usrPhoto": null
}
],
"address": [
{
"address": null
}
]
}
我想做的是将所有内容都放在单个数组中,如下面的结构
{
"profileStatus": [
{
"Total percentage": "50.00",
"Fields blank": 4,
"status": false,
"lastName": null,
"userPhoto": null,
"addressId": null
}
]
}
答案 0 :(得分:1)
这是让你入门的东西,它只是使用一个简单的foreach循环将alle子数组合并为一个。
// your test data as an PHP array, if you have JSON you can convert it with json_decode($jsonString, true)
$a = [
"profileStatus" => [
"Total percentage" => "50.00",
"Fields blank" => 4
],
"lname" => [
"lastName" => null
],
"photo" => [
"usrPhoto" => null
],
"address" => [
"address" => null
]
];
// simple merge loop
$b = [];
foreach ($a as $value) {
$b = array_merge($b, $value);
}
// if you really need the "profileStatus" element you can add it back in
$b = [ "profileStatus" => $b ];
print_r($b);
输出:
Array
(
[profileStatus] => Array
(
[Total percentage] => 50.00
[Fields blank] => 4
[lastName] =>
[usrPhoto] =>
[address] =>
)
)