Php multi-dimensional array assign value to key, change to this effect, how to do
"Image ":[
{
"Local_path": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg ",
"Picture_id": 1
},
{
"Local_path": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg ",
"Picture_id": 2
},
{
"Local_path": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg ",
"Picture_id": 3
}
]
Changed:
"Image ":[
{
"1": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg ",
"Picture_id": 1
},
{
"2": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg ",
"Picture_id": 2
},
{
"3": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg ",
"Picture_id": 3
}
]
Reply to discussion (solution)
Note that ": 1, 2, 3 is the value of picture.
'aaaaa','picture_id' =>1),array('local_path'=>'bbbbbbb','picture_id'=>2),array('local_path'=>'ccccccc','picture_id'=>3),);function change($arrr,$a=0){foreach($arrr as $key=>$value){$arr[$a][$value['picture_id']]=$value['local_path'];$arr[$a]['picture_id']=$value['picture_id'];$a++;}return $arr;}$arry=change($arr);print_r($arry);?>
'aaaaa','picture_id' =>1),array('local_path'=>'bbbbbbb','picture_id'=>2),array('local_path'=>'ccccccc','picture_id'=>3),);function change($arrr,$a=0){foreach($arrr as $key=>$value){$arr[$a][$value['picture_id']]=$value['local_path'];$arr[$a]['picture_id']=$value['picture_id'];$a++;}return $arr;}$arry=change($arr);print_r($arry);?>
It turns around. But what if it is an object? How to Convert
You are not an array of php, it is a json
$s =<<< TXT "image": [ { "local_path": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg", "picture_id": 1 }, { "local_path": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg", "picture_id": 2 }, { "local_path": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg", "picture_id": 3 } ]TXT;$a = json_decode('{'. $s .'}', 1);foreach($a['image'] as &$item) { $item = array( $item['picture_id'] => $item['local_path'], 'picture_id' => $item['picture_id'] );}print_r($a);
Array( [image] => Array ( [0] => Array ( [1] => /upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg [picture_id] => 1 ) [1] => Array ( [2] => /upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg [picture_id] => 2 ) [2] => Array ( [3] => /upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg [picture_id] => 3 ) ))
Restore to json
echo json_encode($a, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
{ "image": [ { "1": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg", "picture_id": 1 }, { "2": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg", "picture_id": 2 }, { "3": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg", "picture_id": 3 } ]}
'aaaaa','picture_id' =>1),array('local_path'=>'bbbbbbb','picture_id'=>2),array('local_path'=>'ccccccc','picture_id'=>3),);function change($arrr,$a=0){foreach($arrr as $key=>$value){$arr[$a][$value['picture_id']]=$value['local_path'];$arr[$a]['picture_id']=$value['picture_id'];$a++;}return $arr;}$arry=change($arr);print_r($arry);?>
What if $ arr is an object instead of an array?
It can be processed as a string. You cannot accept the methods that Zhuning gives you. use regular expressions as strings for processing.