数组项的组合

时间:2012-11-07 18:00:34

标签: php loops for-loop

我遇到了高级循环的问题,这是我的数组

$array1 = array(1,2,3);
$array2 = array(4,5);
$array3 = array(6,7,8,9);
$array4 = array(10,11);

我想循环使用以下结果:

  1. 1,4,6,10
  2. 1,4,6,11
  3. 1,4,7,10-
  4. -1,5,7,11-
  5. 最后:3,5,8,11

    我该怎么做?

5 个答案:

答案 0 :(得分:1)

应该这样做......?

foreach ($array1 as $first) {
  foreach ($array2 as $second) {
    foreach ($array3 as $third) {
      foreach ($array4 as $fourth) {
        echo "$first $second $third $fourth";
      }
    }
  }
}

答案 1 :(得分:1)

可变数量的数组的解决方案。可能写得有点短,但我把它留给你。 ;)

<?php

$array1 = array(1,2,3);
$array2 = array(4,5);
$array3 = array(6,7,8,9);
$array4 = array(10,11);

function turboArray()
{
    $arrays = func_get_args();
    $indexes = array_fill(0, count($arrays), 0);
    function turboSubArray(&$arrays, &$indexes, $l){
        for ($a = 0; $a < count($arrays[$l]); $a++){
            $indexes[$l] = $a;
            if ($l == count($arrays) - 1) {
                for ($i = 0; $i < count($indexes); $i++)
                {
                    echo $arrays[$i][$indexes[$i]];
                    if ($i == count($indexes) - 1){
                        echo "\n<br/>";
                        $indexes[$i] = 0;
                        if ($i == 0) return;
                    }
                    else {
                        echo ', ';
                    }
                }
            } else if ($l < count($indexes)) {
                turboSubArray($arrays, $indexes, $l + 1);
            }
        }
    }
    $l = 0;
    turboSubArray($arrays, $indexes, $l);
}

turboArray($array1, $array2, $array3, $array4);

答案 2 :(得分:0)

我很无聊,决定过度工程。享受。

class CrossJoin implements Iterator {
    private $arrays = array();
    private $index = 0;

    public function __construct(/* $array1, ... */) {
        foreach (func_get_args() as $arr) {
            // ArrayIterator's iteration stuff is cleaner than arrays',
            // and less prone to breakage when objects are passed around
            if (count($arr)) $this->arrays[] = new ArrayIterator($arr);
        }

        // if any of the arrays were empty, a cross join should fail.
        // fudge a single empty array to prevent special-casing later
        if (count($this->arrays) !== func_num_args()) {
            $this->arrays = array(new ArrayIterator(array()));
        }
    }

    public function next() {
        for ($i = count($this->arrays) - 1; $i >= 0; --$i) {
             // carry til we don't have to anymore
             $place = $this->arrays[$i];
             $place->next();
             if ($place->valid()) break;
        }
        // if $i<0, then we carried right off the edge of the list.
        // don't let $arrays[0] be reset, cause that's our indicator
        // that we're done.
        if ($i<0) return;

        ++$this->index;
        for (++$i; $i < count($this->arrays); ++$i) {
             $this->arrays[$i]->rewind();
        }
    }

    public function current() {
        $result = array();
        foreach ($this->arrays as $arr) { $result[] = $arr->current(); }
        return $result;
    }

    public function key() { return $this->index; }

    public function valid() {
        return !empty($this->arrays) && $this->arrays[0]->valid();
    }

    public function rewind() {
        foreach ($this->arrays as $arr) $arr->rewind();
        $this->index = 0;
    }
}

$combos = new CrossJoin(
    array(1, 2, 3),
    array(4, 5),
    array(6, 7, 8, 9),
    array(10, 11)
);

foreach ($combos as $combo) {
    echo implode(', ', $combo), "\n";
}

答案 3 :(得分:-1)

希望有所帮助:

for ($i = 0; $i < count($a1); ++i) {
  for ($j = 0; $j < count($a2); ++j) {
    for ($k = 0; $k < count($a3); ++k) {
      for ($l = 0; $l < count($a4); ++l) {
        echo $a1[$i] . ' ' . $a2[$j] . ' ' . $a3[$k] . ' ' . $a4[$l] . '\n';
      }
    }
  }
}

干杯!

答案 4 :(得分:-2)

您正在寻找PHP array_merge功能。