tags:

views:

47

answers:

4

I am sure this is really easy, but I can't find the right function.

I have two arrays, one for x values, one for y and now I want to combine them xyxyxy.

for example:

$x = array( 0=>10, 1=>20, 2=>30 );

$y = array( 0=>15, 1=>25, 2=>35 );

Mixed would leave:

$xy = array( 0=>10, 1=>15, 2=>20, 3=>25, 4=>30, 5=>35 );

A: 

try this

$a = array_merge($x, $y);
asort($a);
print_r($a);
antpaw
The x and y won't always be in numerical order that was just to make it easier to see the 'before and after'
Mark
+1  A: 
$x = array( 0=>10, 1=>20, 2=>30 );
$y = array( 0=>15, 1=>25, 2=>35 );
$xy = array();
for ($i=0; $i<count(x); $i++) {
  $xy[] += $x[i];
  $xy[] += $y[i];
}
cletus
+1  A: 

If you can't rely on the keys matching across both arrays you could try something like the following

 $x = array("XA" => "X 1", "XB" => "X 2", "XC" => "X 3");
 $y = array("YA" => "Y 1", "YB" => "Y 2", "YC" => "Y 3");
 $xy = array();
 foreach($x as $k => $v) {
  $xy[] = array_shift($x);
  $xy[] = array_shift($y);
 }
frglps