如何从此json创建一个将ATTRIBUTE_TYPE与ATTRIBUTE ID相关联的数组。 现在我正在返回以下json:
(
[success] => 1
[ATTRIBUTES_COUNT] => 106
[ATTRIBUTES] => Array
(
[0] => Array
(
[ATTRIBUTE_TYPE] => email
[DATA_TYPE] => CHAR
[COMPARE_COUNT] => 2
[COMPARE] => Array
(
[0] => Array
(
[ATTRIBUTE_ID] => 4222
[DESCR] => Equals
)
[1] => Array
(
[ATTRIBUTE_ID] => 4223
[DESCR] => Not Equal
)
)
)
[1] => Array
(
[ATTRIBUTE_TYPE] => interest
[DATA_TYPE] => MONEY
[COMPARE_COUNT] => 7
[COMPARE] => Array
(
[0] => Array
(
[ATTRIBUTE_ID] => 4006
[COMPARE_DESCR] => Not Equal
)
[1] => Array
(
[ATTRIBUTE_ID] => 4000
[COMPARE_DESCR] => Equals
)
[2] => Array
(
[ATTRIBUTE_ID] => 4001
[COMPARE_DESCR] => Greater Than
)
)
)
)
现在我想创建一个在json_decode之后返回类似内容的数组:
{
"DETAILS": [
{
"ATTRIBUTE_ID": "4222",
"ATTRIBUTE_TYPE": "email",
},
{
"ATTRIBUTE_ID": "4006",
"ATTRIBUTE_TYPE": "interest",
}
]
}
相同的属性类型可以有多个属性ID。
答案 0 :(得分:1)
它不是特别优雅,但试一试($ inputArray是你在问题顶部发布的深层嵌套数组):
$desiredArray = array("DETAILS" => array());
foreach($inputArray["ATTRIBUTES"] as $attribute)
{
foreach($attribute["COMPARE"] as $compare)
{
$desiredArray["DETAILS"][] = array("ATTRIBUTE_ID" => $compare["ATTRIBUTE_ID"],
"ATTRIBUTE_TYPE" => $attribute["ATTRIBUTE_TYPE"]);
}
}
$desiredJson = json_encode($desiredArray);