PHP如何对json对象进行分组

时间:2022-01-04 16:49:32

i want to ask about json object

我想问一下json对象

i have json object like this:

我有像这样的json对象:

{"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC01","TASK_NAME":"Human service 1"}
{"ID":"MM","ID_NAME":"Management","TASK_ID":"MM01","TASK_NAME":"Management 1"}
{"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC02","TASK_NAME":"Human service 2"}
{"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC03","TASK_NAME":"Human service 3"}
{"ID":"QC","ID_NAME":"Quality Control","TASK_ID":"QC01","TASK_NAME":"Quality Control 1"}
{"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC04","TASK_NAME":"Human service 4"}

and i would like to group json object which has same id, id_name. For example like this

我想分组具有相同id,id_name的json对象。比如这样

{"ID":"HC","ID_NAME":"Human Capital","items": [
     {"TASK_ID":"HC01","TASK_NAME":"Human service 1"},
     {"TASK_ID":"HC02","TASK_NAME":"Human service 2"},
     {"TASK_ID":"HC03","TASK_NAME":"Human service 3"},
     {"TASK_ID":"HC04","TASK_NAME":"Human service 4"}
   ]
},
{"ID":"MM","ID_NAME":"Management","items": [
     {"TASK_ID":"MM01","TASK_NAME":"Managemen 1"}
   ]
},
{"ID":"QC","ID_NAME":"Quality Control","items": [
     {"TASK_ID":"QC01","TASK_NAME":"Quality Control 1"}
   ]
},

please help me with my study.

请帮助我学习。

1 个解决方案

#1


1  

We decode the JSON, loop through the objects, add unique IDs to a new array, create a placeholder array for ITEMS, and append the TASK to this ITEMS. After this is all done, we can re-encode the data and return.

我们解码JSON,遍历对象,向新阵列添加唯一ID,为ITEMS创建占位符数组,并将TASK附加到此项目。完成所有操作后,我们可以重新编码数据并返回。

Code:

码:

// Your JSON array of objects
$json = <<<JSON
[
  {"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC01","TASK_NAME":"Human service 1"},
  {"ID":"MM","ID_NAME":"Management","TASK_ID":"MM01","TASK_NAME":"Management 1"},
  {"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC02","TASK_NAME":"Human service 2"},
  {"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC03","TASK_NAME":"Human service 3"},
  {"ID":"QC","ID_NAME":"Quality Control","TASK_ID":"QC01","TASK_NAME":"Quality Control     1"},
  {"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC04","TASK_NAME":"Human service 4"}
]
JSON;

// Decode your JSON and create a placeholder array
$objects = json_decode($json);
$grouped = array();

// Loop JSON objects
foreach($objects as $object) {
    if(!array_key_exists($object->ID, $grouped)) { // a new ID...
         $newObject = new stdClass();

         // Copy the ID/ID_NAME, and create an ITEMS placeholder
         $newObject->ID = $object->ID;
         $newObject->ID_NAME = $object->ID_NAME;
         $newObject->ITEMS = array();

         // Save this new object
         $grouped[$object->ID] = $newObject;
    }

    $taskObject = new stdClass();

    // Copy the TASK/TASK_NAME
    $taskObject->TASK_ID = $object->TASK_ID;
    $taskObject->TASK_NAME = $object->TASK_NAME;

    // Append this new task to the ITEMS array
    $grouped[$object->ID]->ITEMS[] = $taskObject;
}

// We use array_values() to remove the keys used to identify similar objects
// And then re-encode this data :)
$grouped = array_values($grouped);
$json = json_encode($grouped);

Output:

输出:

PHP如何对json对象进行分组

Documentation:

文档:

#1


1  

We decode the JSON, loop through the objects, add unique IDs to a new array, create a placeholder array for ITEMS, and append the TASK to this ITEMS. After this is all done, we can re-encode the data and return.

我们解码JSON,遍历对象,向新阵列添加唯一ID,为ITEMS创建占位符数组,并将TASK附加到此项目。完成所有操作后,我们可以重新编码数据并返回。

Code:

码:

// Your JSON array of objects
$json = <<<JSON
[
  {"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC01","TASK_NAME":"Human service 1"},
  {"ID":"MM","ID_NAME":"Management","TASK_ID":"MM01","TASK_NAME":"Management 1"},
  {"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC02","TASK_NAME":"Human service 2"},
  {"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC03","TASK_NAME":"Human service 3"},
  {"ID":"QC","ID_NAME":"Quality Control","TASK_ID":"QC01","TASK_NAME":"Quality Control     1"},
  {"ID":"HC","ID_NAME":"Human Capital","TASK_ID":"HC04","TASK_NAME":"Human service 4"}
]
JSON;

// Decode your JSON and create a placeholder array
$objects = json_decode($json);
$grouped = array();

// Loop JSON objects
foreach($objects as $object) {
    if(!array_key_exists($object->ID, $grouped)) { // a new ID...
         $newObject = new stdClass();

         // Copy the ID/ID_NAME, and create an ITEMS placeholder
         $newObject->ID = $object->ID;
         $newObject->ID_NAME = $object->ID_NAME;
         $newObject->ITEMS = array();

         // Save this new object
         $grouped[$object->ID] = $newObject;
    }

    $taskObject = new stdClass();

    // Copy the TASK/TASK_NAME
    $taskObject->TASK_ID = $object->TASK_ID;
    $taskObject->TASK_NAME = $object->TASK_NAME;

    // Append this new task to the ITEMS array
    $grouped[$object->ID]->ITEMS[] = $taskObject;
}

// We use array_values() to remove the keys used to identify similar objects
// And then re-encode this data :)
$grouped = array_values($grouped);
$json = json_encode($grouped);

Output:

输出:

PHP如何对json对象进行分组

Documentation:

文档: