Is it possible to merge two arrays and alternate the elements.
For example
$alph = array("a", "b", "c");
$num = array("1", "2", "3");
I want to merge them, and use a foreach to loop through the merged array and output like:
a1b2c3
merging arrays
Moderator: General Moderators
- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
Code: Select all
$arr1 = arr("color" => "red", 2, 4);
$arr2 = arr("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$res = array_merge($array1, $array2);
print_r($result);
?>Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
that's it
Re: merging arrays
Do not think array_merge will solve the output wanted, so;bluesman333 wrote: I want to merge them, and use a foreach to loop through the merged array and output like:
a1b2c3
Code: Select all
<pre>
<?php
$alph = array("a", "b", "c");
$num = array("1", "2", "3");
for ($i=0;$i<count($alph);$i++) {
$arr[] = $alph[$i];
$arr[] = $num[$i];
}
print_r($arr); // debugging to demonstrate
foreach($arr as $key => $val) {
echo $val;
}
?>Code: Select all
Array
(
ї0] => a
ї1] => 1
ї2] => b
ї3] => 2
ї4] => c
ї5] => 3
)
a1b2c3