Hi
I have to arrays, a and b. Array a have the following keys: 0,2 and 4.
Array b have keys 1, 3 and 5.
What I want to do is reset the keys in a so they equal 0, 1 and 2, and in b to 3, 4 and 5.
Finally the two arrays should be merged into one with keys 0, 1, 2, 3, 4 and 5.
Can this be done in a simple way?
array_merge?
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
asort($newarray = array_merge($a,$b));err.. sorry..
Code: Select all
ksort($newarray = array_merge($a,$b))ksort() should do it... Thanks
[edit] or not... The order is still ababab.
Something like this should work... Haven't tested it yet.
[edit] or not... The order is still ababab.
Something like this should work... Haven't tested it yet.
Code: Select all
<?php
$key = 0;
while($tmp = each($a))
$newa[$key++] = $tmp['value'];
while($tmp = each($b))
$newb[$key++] = $tmp['value'];
$newarray = array_merge($newa, $newb);
?>Did I misunderstand this? If so, I appologize... If not:
Result:
Code: Select all
$a = array (
0 => 'foo', 2 => 'bar', 4 => 'moo'
);
$b = array (
1 => 'c', 3 => 'a', 5 => 'b'
);
$new_arr= array_merge_recursive($a, $b);
print_r($new_arr);Code: Select all
Array
(
ї0] => foo
ї1] => bar
ї2] => moo
ї3] => c
ї4] => a
ї5] => b
)