Php 选择多个数组元素

Php 选择多个数组元素,php,arrays,select,elements,Php,Arrays,Select,Elements,PHP中是否有一种方法可以一次选择多个数组元素,例如,在for循环中,$i=要选择的第一个集合的大小,然后后续增量表示从数组中选择该大小的下一个集合- 谢谢 如果我没弄错你的问题,你有这样的问题吗 $array = array( "A" => array("a","b"), "B" => array("a","b"), "C" => array("a","b")); 你想同时循环思想A,B,C吗 然后你可以做这样

PHP中是否有一种方法可以一次选择多个数组元素,例如,在for循环中,$i=要选择的第一个集合的大小,然后后续增量表示从数组中选择该大小的下一个集合-


谢谢

如果我没弄错你的问题,你有这样的问题吗

$array = array( "A" => array("a","b"),
                "B" => array("a","b"),
                "C" => array("a","b"));
你想同时循环思想A,B,C吗

然后你可以做这样的事情

for($i=0;$i<=max(count($array['A']),count($array['B']),count($array['B']))){
     if(count($array['A'])<=$i+1) {
         echo $array['A'][$i];
     }
     if(count($array['B'])<=$i+1) {
         echo $array['B'][$i];
     }
     if(count($array['B'])<=$i+1) {
         echo $array['B'][$i];
     }
     $i++;
}

如果我没弄错你的问题,你有这样的问题吗

$array = array( "A" => array("a","b"),
                "B" => array("a","b"),
                "C" => array("a","b"));
你想同时循环思想A,B,C吗

然后你可以做这样的事情

for($i=0;$i<=max(count($array['A']),count($array['B']),count($array['B']))){
     if(count($array['A'])<=$i+1) {
         echo $array['A'][$i];
     }
     if(count($array['B'])<=$i+1) {
         echo $array['B'][$i];
     }
     if(count($array['B'])<=$i+1) {
         echo $array['B'][$i];
     }
     $i++;
}

也就是说,不是一次只循环一个数组元素,而是循环选择对,例如3个元素,然后对这3个元素执行操作

有很多方法可以做到这一点。 一个是

$arr = array(1,2,3,4,5,6,7,8,9);
$new = array_chunk($arr,3);
foreach ($new as $chunk) {
  print_r($chunk);// 3 elements to do something with
}

也就是说,不是一次只循环一个数组元素,而是循环选择对,例如3个元素,然后对这3个元素执行操作

有很多方法可以做到这一点。 一个是

$arr = array(1,2,3,4,5,6,7,8,9);
$new = array_chunk($arr,3);
foreach ($new as $chunk) {
  print_r($chunk);// 3 elements to do something with
}

这取决于您希望如何对元素进行分组

$i = 4;
$source = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 );
// If you want consecutive elements in the same group, i.e. the first $i elements etc
$chunks = array_chunk( $source, $i );
foreach( $chunks as $chunk )
{
    // Iterate over chunk
    echo '---<br />';
    foreach( $chunk as $element )
    {
        echo $element . '<br />';
    }
}
echo '---<br />';
echo '---<br />';
// Otherwise if you want consecutive elements in separate groups
$lastElement = count( $source ) - 1;
$step = ceil( count( $source) / $i );
for( $offset = 0; $offset < $step; $offset++ )
{
    echo '---<br />';
    for( $element = $offset; $element <= $lastElement; $element+= $step )
    {
        echo $source[$element] . '<br />';
    }
}
echo '---<br />';

这取决于您希望如何对元素进行分组

$i = 4;
$source = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 );
// If you want consecutive elements in the same group, i.e. the first $i elements etc
$chunks = array_chunk( $source, $i );
foreach( $chunks as $chunk )
{
    // Iterate over chunk
    echo '---<br />';
    foreach( $chunk as $element )
    {
        echo $element . '<br />';
    }
}
echo '---<br />';
echo '---<br />';
// Otherwise if you want consecutive elements in separate groups
$lastElement = count( $source ) - 1;
$step = ceil( count( $source) / $i );
for( $offset = 0; $offset < $step; $offset++ )
{
    echo '---<br />';
    for( $element = $offset; $element <= $lastElement; $element+= $step )
    {
        echo $source[$element] . '<br />';
    }
}
echo '---<br />';

也就是说,不是一次只循环一个数组元素,而是循环选择的对,例如3个元素,然后对这3个元素做一些事情。您研究过php数组_切片函数吗?这就是你要找的吗?对于$i=0$iI.e。不是一次只循环一个数组元素,而是循环选择对,例如3个元素,然后对这3个元素做些什么。你有没有研究过php array_slice函数?这就是你要找的吗?对于$i=0$我