PHP multidimensional array to assign value 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
}
]
Change to:
"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 ": The value of the picture is
' 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);p rint_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);p rint_r ($arry);? >
It really turned around. But what if it's an object? How to turn
You're not an array of PHP, it's 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 )))
Revert back 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);p rint_r ($arry);? >
What if $arr is an object, not an array?
When the string is processed. Zu Ning give you the method you can not accept, then use the regular as a string processing it