<?php
$json_string = '{"error_code":0,"error_message":"","return_data":{"items":[{"id":462,"users":"1,36,38"},{"id":462,"users":"1,4"}]}}';
$res = json_decode($json_string, 1);
$items = $res['return_data']['items'];
unset($res['return_data']['items']); // unset it first, to replace a new one
$new_items = array();
foreach ($items as $key => $item) {
$usersArray['id'] = $item['id'];
$usersArray['users'] = array(); //create a new Array
$usersIdArray = explode(',', $item['users']);
foreach ($usersIdArray as $key => $user) {
$usersArray['users'][] = array('user_id' => $user);
// another level ^
}
$res['return_data']['items'][] = $usersArray;
}
echo json_encode($res, JSON_PRETTY_PRINT);
preferences:
23.78 ms | 406 KiB | 5 Q