开发者

Mixing two arrays [duplicate]

This question already has answers here: Merge two flat indexed arrays of equal size so that values are pushed into the result in an alternating fashion 开发者_如何学Python (2 answers) Closed 9 years ago.

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 );


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);
 }


$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];
}


try this

$a = array_merge($x, $y);
asort($a);
print_r($a);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜