Php multi-dimensional array assigns value to key

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.