我需要重新格式化我的JSON文件,以便数组中的值现在是该数组的键。
转{
"ID": "M-420",
"ProductName": "example product name ",
"ProductDescription": "example description",
"Color": "blue "
},
{
"ID": "M-421",
"ProductName": "example product name ",
"ProductDescription": "example description",
"Color": "yellow "
}
到
{
"M-420": {
"ProductName": "example product name ",
"ProductDescription": "example description",
"Color": "blue "
}
},
{
"M-421": {
"ProductName": "example product name ",
"ProductDescription": "example description",
"Color": "blue "
}
}
我需要能够使用PHP抓取每个唯一ID的属性。我正在使用$json_decode();
感谢!
$new_array = array();
foreach ($array as $element) {
$new_array[$element['ID']] = $element;
}
很简单的解决方案是:
$json_obj_old = json_decode($json_before_process);
$json_obj_new = array();
foreach($json_obj_old as $json_element){
$json_obj_new[$json_element['ID']] = $json_element;
unset($json_obj_new[$json_element['ID']]['ID']);
}
return json_encode($json_obj_new);