PHP如何合并二维数组

WBOY
Release: 2016-06-23 13:32:11
Original
979 people have browsed it

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

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

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


回复讨论(解决方案)

有人吗??
这个点

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

$t['arr'] = array_map('current', $t['arr']);echo json_encode($t);
Copy after login

<?php$o = <<<FDIPZONE{"arr": [                {                    "a": "XXX"                },                {                    "a": "YYY"                }]}FDIPZONE;$arr = json_decode($o, true);$tmp = array();foreach($arr['arr'] as $k=>$v){    array_push($tmp, $v['a']);}$result = array('arr'=>array($tmp));header('content-type:application/json');echo json_encode($result, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);?>
Copy after login

Related labels:
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