我有这个数组:
$array = array(a, b, c, d, e, f, g);
我希望将它分成两个数组,具体取决于索引是偶数还是奇数,如下所示:
$odd = array(a, c, e, g);
$even = array(b, d, f);
提前致谢!
答案 0 :(得分:33)
一种解决方案,使用匿名函数和array_walk
:
$odd = array();
$even = array();
$both = array(&$even, &$odd);
array_walk($array, function($v, $k) use ($both) { $both[$k % 2][] = $v; });
这样就可以在数组中仅一次分隔项目,但它在“聪明”方面有点过分。它并不比经典,更冗长的
更好$odd = array();
$even = array();
foreach ($array as $k => $v) {
if ($k % 2 == 0) {
$even[] = $v;
}
else {
$odd[] = $v;
}
}
答案 1 :(得分:9)
使用array_filter
(PHP> = 5.6):
$odd = array_filter($array, function ($input) {return $input & 1;}, ARRAY_FILTER_USE_KEY);
$even = array_filter($array, function ($input) {return !($input & 1);}, ARRAY_FILTER_USE_KEY);
答案 2 :(得分:4)
我不确定这是否是最优雅的方式,但它应该有魅力:
$odd=array();
$even=array();
$count=1;
foreach($array as $val)
{
if($count%2==1)
{
$odd[]=$val;
}
else
{
$even[]=$val;
}
$count++;
}
答案 3 :(得分:2)
作为一个近乎一线的人,我认为这将是我的最爱:
$even = $odd = array();
foreach( $array as $k => $v ) $k % 2 ? $odd[] = $v : $even[] = $v;
还是一点点?速度:
$even = $odd = array();
foreach( $array as $k => $v ) ( $k & 1 ) === 0 ? $even[] = $v : $odd[] = $v;
更冗长的变体:
$both = array( array(), array() );
// or, if $array has at least two elements:
$both = array();
foreach( $array as $k => $v ) $both[ $k % 2 ][] = $v;
list( $even, $odd ) = $both;
使用array_chunk
:
$even = $odd = array();
foreach( array_chunk( $array, 2 ) as $chunk ){
list( $even[], $odd[] ) = isset( $chunk[1]) ? $chunk : $chunk + array( null, null );
// or, to force even and odd arrays to have the same count:
list( $even[], $odd[] ) = $chunk + array( null, null );
}
如果$ array保证具有偶数个元素:
$even = $odd = array();
foreach( array_chunk( $array, 2 ) as $chunk )
list( $even[], $odd[] ) = $chunk;
PHP 5.5.0+与array_column
:
$chunks = array_chunk( $array, 2 );
$even = array_column( $chunks, 0 );
$odd = array_column( $chunks, 1 );
旧版PHP版本的类似内容。
键将是0,2,4,...和1,3,5,....如果您不喜欢这样,请同时应用array_values
:
$even = array_intersect_key( $array, array_flip( range( 0, count( $array ), 2 )));
$odd = array_intersect_key( $array, array_flip( range( 1, count( $array ), 2 )));
或
$even = array_intersect_key( $array, array_fill_keys( range( 0, count( $array ), 2 ), null ));
$odd = array_intersect_key( $array, array_fill_keys( range( 1, count( $array ), 2 ), null ));
答案 4 :(得分:1)
只需循环遍历它们并检查键是偶数还是奇数:
$odd = array();
$even = array();
foreach( $array as $key => $value ) {
if( 0 === $key%2) { //Even
$even[] = $value;
}
else {
$odd[] = $value;
}
}
答案 5 :(得分:1)
一
$odd = $even = array();
for ($i = 0, $l = count($array ); $i < $l;) { // Notice how we increment $i each time we use it below, by two in total
$even[] = $array[$i++];
if($i < $l)
{
$odd[] = $array[$i++];
}
}
两个
$odd = $even = array();
foreach (array_chunk($array , 2) as $chunk) {
$even[] = $chunk[0];
if(!empty( $chunk[1]))
{
$odd[] = $chunk[1];
}
}
答案 6 :(得分:1)
根据@ Jon的第二个变体,我在之后使用PHP Smarty v3模板引擎。这是用于显示带有一列或两列模板模型的新闻/博客。
在MySql查询之后,我将执行以下代码:
if(sizeof($results) > 0) {
$data = array();
foreach($results as $k => $res) {
if($k % 2 == 0) {
$res["class"] = "even";
$data["all"][] = $data["even"][] = $res;
}
else {
$res["class"] = "odd";
$data["all"][] = $data["odd"][] = $res;
}
}
}
我使用Smarty语法获得了一个包含3个子数组(包括奇数/偶数类)的数组:
{foreach $data.all as $article}...{/foreach}
{foreach $data.odd as $article}...{/foreach}
{foreach $data.even as $article}...{/foreach}
希望它可以帮助一些人...
答案 7 :(得分:1)
$odd = [];
$even = [];
while (count($arr)) {
$odd[] = array_shift($arr);
$even[] = array_shift($arr);
}
答案 8 :(得分:0)
<?php
$array1 = array(0,1,2,3,4,5,6,7,8,9);
$oddarray = array();
$evenarray = array();
$count = 1;
echo "Original: ";
foreach ($array1 as $value)
{
echo "$value";
}
echo "<br> Even: ";
foreach ($array1 as $print)
{
if ($count%2==1)
{
$evenarray = $print;
echo "$print";
}
$count++;
}
echo "<br> Odd: ";
foreach ($array1 as $print2) {
if ($count%2!=1)
{
$oddarray[] = $print2;
echo "$print2";
}
$count++;
}
?>
Output:
Original: 0123456789
Even: 02468
Odd: 13579
答案 9 :(得分:0)
使用array_chunk和array_map的另一种功能解决方案。当源数组的大小为奇数时,最后一行从第二个数组中删除空项
list($odd, $even) = array_map(null, ...array_chunk($ar,2));
if(count($ar) % 2) array_pop($even);