Home > Backend Development > PHP Tutorial > 关于数组合并的有关问题

关于数组合并的有关问题

WBOY
Release: 2016-06-13 12:54:43
Original
1018 people have browsed it

关于数组合并的问题
原数组:

<br />
$args1 = Array(<br />
		'0' => array(<br />
				'0' => 'a',<br />
				'1' => array(<br />
					'0' => array ( 'col1' => 'A1', 'col2' => 0 )<br />
				)<br />
		),<br />
		'1' => array(<br />
				'0' => 'b',<br />
				'1' => array(<br />
					'0' => array ( 'col1' => 'B1', 'col2' => 1 )<br />
				)<br />
		)<br />
);<br />
$args2 = Array(<br />
		'0' => array(<br />
				'0' => 'a',<br />
				'1' => array(<br />
					'0' => Array ( 'col1' => 'A2', 'col2' => 0 ), <br />
					'1' => Array ( 'col1' => 'A3', 'col2' => 0 ) <br />
				)<br />
		)<br />
);<br />
Copy after login


目标数组:
<br />
Array(<br />
		'0' => array(<br />
				'0' => 'a',<br />
				'1' => array(<br />
					'0' => array ( 'col1' => 'A1', 'col2' => 0 ),<br />
                                        '1' => Array ( 'col1' => 'A2', 'col2' => 0 ), <br />
					'2' => Array ( 'col1' => 'A3', 'col2' => 0 ) <br />
				)<br />
		),<br />
		'1' => array(<br />
				'0' => 'b',<br />
				'1' => array(<br />
					'0' => array ( 'col1' => 'B1', 'col2' => 1 )<br />
				)<br />
		)<br />
);<br />
Copy after login


------解决方案--------------------
$res = array();<br />
foreach($args1 as $item) $res[$item[0]] = $item;<br />
foreach($args2 as $item) $res[$item[0]][1] = array_merge($res[$item[0]][1], $item[1]);<br />
<br />
print_r(array_values($res));
Copy after login
Array
(
    [0] => Array
        (
            [0] => a
            [1] => Array
                (
                    [0] => Array
                        (
                            [col1] => A1
                            [col2] => 0
                        )

                    [1] => Array
                        (

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