dreamfly2016 2009-12-21 23:25
浏览 4

PHP-ILooping数组值通过一个更大的数组

I want to know if it is possible to take an array and insert the array's values into a bigger array, multiple times, so that the values of the smaller array fill up the bigger array.

Say array1 has values ([0 => 'a'],[1 => 'b'],[2 => 'c']), and array2 can hold 8 values. So, how would I take the values of array1 and insert them into array2 continuously until array2 runs out of space, so that array2 would have the values 'a','b','c','a','b','c','a','b'?

Thanks in advance, ~Hussain~

  • 写回答

4条回答 默认 最新

  • dousha7904 2009-12-21 23:28
    关注

    Essentially, you want to loop over and over the small array, adding each element to the new array until it has reached the desired size.

    Consider this:

    $max = 8;
    
    $Orig_Array = array('a', 'b', 'c');
    $Next_Array = array();
    
    while True
    {
        foreach($Orig_Array as $v)
        {
           $Next_Array[] = $v;
           if(count($Next_Array) >= $max)
              break 2;
        }
    }
    
    评论

报告相同问题?