Home > Backend Development > PHP Tutorial > php多维数组把value值赋值到key

php多维数组把value值赋值到key

WBOY
Release: 2016-06-23 13:41:03
Original
1407 people have browsed it

php多维数组把value值赋值到key,改成这种效果,怎么做
   "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
                }
            ]
改成:
   "image": [
                {
                    "1": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",
                    "picture_id": 1
                },
                {
                    "2": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",
                    "picture_id": 2
                },
                {
                    "3": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",
                    "picture_id": 3
                }
            ]


回复讨论(解决方案)

注意“:1,2,3是picture的值

<?php$arr=array(			array(				'local_path' =>'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);					?>	
Copy after login
Copy after login
Copy after login

<?php$arr=array(			array(				'local_path' =>'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);					?>	
Copy after login
Copy after login
Copy after login


这样的确转过来了。但如果是对象呢?怎么转

你这不是 php 的数组,是 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);
Copy after login
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                )        ))
Copy after login
还原回 json
echo json_encode($a, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
Copy after login
{    "image": [        {            "1": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",            "picture_id": 1        },        {            "2": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",            "picture_id": 2        },        {            "3": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",            "picture_id": 3        }    ]}
Copy after login

<?php$arr=array(			array(				'local_path' =>'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);					?>	
Copy after login
Copy after login
Copy after login


如果$arr是对象,而不是数组怎么做呢?

当字符串处理吧。祖宁给你的方法你都无法接受,那就用正则当字符串处理吧

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template