Rumah > pembangunan bahagian belakang > tutorial php > php多维数组把value值赋值到key,该怎么处理

php多维数组把value值赋值到key,该怎么处理

WBOY
Lepaskan: 2016-06-13 12:14:16
asal
2301 orang telah melayarinya

php多维数组把value值赋值到key
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
                }
            ]
------解决思路----------------------

<br /><br /><?php<br /><br /><br />$arr=array(<br />			array(<br />				'local_path' =>'aaaaa',<br />				'picture_id' =>1<br />			),<br />			array(<br />				'local_path'=>'bbbbbbb',<br />				'picture_id'=>2<br />			),<br />			array(<br />				'local_path'=>'ccccccc',<br />				'picture_id'=>3<br />			),<br />		);<br /><br />	function change($arrr,$a=0){<br />		foreach($arrr as $key=>$value){<br />			$arr[$a][$value['picture_id']]=$value['local_path'];<br />			$arr[$a]['picture_id']=$value['picture_id'];<br />			$a++;		<br />		<br />		}<br />	return $arr;<br />	}<br />		<br />		<br />		$arry=change($arr);<br />		<br />		print_r($arry);<br />		<br />		<br />	?>	<br />
Salin selepas log masuk


------解决思路----------------------
你这不是 php 的数组,是 json
$s =<<< TXT<br /> "image": [<br />                {<br />                    "local_path": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",<br />                    "picture_id": 1<br />                },<br />                {<br />                    "local_path": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",<br />                    "picture_id": 2<br />                },<br />                {<br />                    "local_path": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",<br />                    "picture_id": 3<br />                }<br />            ]<br />TXT;<br />$a = json_decode('{'. $s .'}', 1);<br /><br />foreach($a['image'] as &$item) {<br />  $item = array(<br />    $item['picture_id'] => $item['local_path'],<br />    'picture_id' => $item['picture_id']<br />    );<br />}<br />print_r($a);
Salin selepas log masuk
Array<br />(<br />    [image] => Array<br />        (<br />            [0] => Array<br />                (<br />                    [1] => /upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg<br />                    [picture_id] => 1<br />                )<br /><br />            [1] => Array<br />                (<br />                    [2] => /upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg<br />                    [picture_id] => 2<br />                )<br /><br />            [2] => Array<br />                (<br />                    [3] => /upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg<br />                    [picture_id] => 3<br />                )<br /><br />        )<br /><br />)<br />
Salin selepas log masuk
还原回 json
echo json_encode($a, JSON_PRETTY_PRINT <br><font color='#FF8000'>------解决思路----------------------</font><br> JSON_UNESCAPED_SLASHES);
Salin selepas log masuk
{<br />    "image": [<br />        {<br />            "1": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",<br />            "picture_id": 1<br />        },<br />        {<br />            "2": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",<br />            "picture_id": 2<br />        },<br />        {<br />            "3": "/upload/14d4aaf3c5ef5b93e6c4c7418428ea71.jpg",<br />            "picture_id": 3<br />        }<br />    ]<br />}<br />
Salin selepas log masuk

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan