Home  >  Article  >  Backend Development  >  PHP怎么合并二维数组

PHP怎么合并二维数组

WBOY
WBOYOriginal
2016-06-13 12:22:221410browse

PHP如何合并二维数组
"arr": [
                {
                    "a": "XXX"
                },
                {
                    "a": "YYY"
                }
]

这是前段print_r出来的数据
我想改成:

"arr": [
                {
                     "XXX",
                    "YYY"
                }
]

------解决思路----------------------

$s =<<< TXT<br />{"arr": [<br />                {<br />                    "a": "XXX"<br />                },<br />                {<br />                    "a": "YYY"<br />                }<br />]<br />}<br />TXT;<br /><br />$t = json_decode($s, 1);<br />array_walk($t['arr'], function(&$v) {$v = current($v);});<br />print_r($t);<br />echo json_encode($t);
Array<br />(<br />    [arr] => Array<br />        (<br />            [0] => XXX<br />            [1] => YYY<br />        )<br /><br />)<br />{"arr":["XXX","YYY"]}<br />

Statement:
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