note 113036 deleted from function.array-merge by salathe
| From: | salathe@php.net | Date: | Mon, 06 Sep 2021 22:45:00 +0000 |
| Subject: | note 113036 deleted from function.array-merge by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-216890@lists.php.net to get a copy of this message | ||
Note Submitter: vladas dot dirzys at gmail dot com
----
To combine several results (arrays):
<?php
$results = array(
array(
array('foo1'),
array('foo2'),
),
array(
array('bar1'),
array('bar2'),
),
);
$rows = call_user_func_array('array_merge', $results);
print_r($rows);
?>
The above example will output:
Array
(
[0] => Array
(
[0] => foo1
)
[1] => Array
(
[0] => foo2
)
[2] => Array
(
[0] => bar1
)
[3] => Array
(
[0] => bar2
)
)
However, example below helps to preserve numeric keys:
<?php
$results = array(
array(
123 => array('foo1'),
456 => array('foo2'),
),
array(
321 => array('bar1'),
654 => array('bar2'),
),
);
$rows = array();
foreach ($results as &$result) {
$rows = $rows + $result; // preserves keys
}
print_r($rows);
?>
The above example will output:
Array
(
[123] => Array
(
[0] => foo1
)
[456] => Array
(
[0] => foo2
)
[321] => Array
(
[0] => bar1
)
[654] => Array
(
[0] => bar2
)
)