note 113036 added to function.array-merge
| From: | vladas dot dirzys at gmail dot com | Date: | Wed, 21 Aug 2013 09:11:08 +0000 |
| Subject: | note 113036 added to function.array-merge | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196037@lists.php.net to get a copy of this message | ||
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
)
)
----
Server IP: 69.147.83.199
Probable Submitter: 92.62.38.2
----
Manual Page -- http://php.net/manual/en/function.array-merge.php
Edit -- https://master.php.net/note/edit/113036
Del: integrated -- https://master.php.net/note/delete/113036/integrated
Del: useless -- https://master.php.net/note/delete/113036/useless
Del: bad code -- https://master.php.net/note/delete/113036/bad+code
Del: spam -- https://master.php.net/note/delete/113036/spam
Del: non-english -- https://master.php.net/note/delete/113036/non-english
Del: in docs -- https://master.php.net/note/delete/113036/in+docs
Del: other reasons-- https://master.php.net/note/delete/113036
Reject -- https://master.php.net/note/reject/113036
Search -- https://master.php.net/manage/user-notes.php