PHP-从一个数组中取4个项,然后从另一个数组取2个项,合并到一个数组,递归直到没有更多项



我有两个数组,一个是横向图像URL,另一个是纵向图像URL。它们最终将以图像幻灯片的形式输出到页面上,每张幻灯片由4张横向图像和2张纵向图像组成。

我已经把图像放入了它们的阵列中,我称之为

$horizontalImages
$verticalImages

现在我想创建一个新的数组,它从$horizontalImages中选择前4个图像,从$verticalImages中选择前2个图像,并在新数组中创建一个条目。然后从$horizontalImages中递归地选择下一个4,从$verticalImages中递归地选下一个2,并在新数组中生成下一个项,并继续进行,直到没有更多项为止。

所以,我认为数据应该是什么样子的天上掉馅饼的例子是这样的,请原谅这里的错误,我正在根据它可能是什么样子来处理它,我甚至不知道这是正确的,还是多维/嵌套数组,所以很明显,我需要帮助理解这个新数组的结构原理。

array(x) {
    [0]=>???Array???(x){
       [0]=>"/landscape-image-1.jpg"
       [1]=>"/landscape-image-2.jpg"
       [2]=>"/landscape-image-3.jpg"
       [3]=>"/landscape-image-4.jpg"
       [4]=>"/portrait-image-1.jpg"
       [5]=>"/portrait-image-2.jpg"
    }
    [1]=>???Array???(x){
       [0]=>"/landscape-image-5.jpg"
       [1]=>"/landscape-image-6.jpg"
       [2]=>"/landscape-image-7.jpg"
       [3]=>"/landscape-image-8.jpg"
       [4]=>"/portrait-image-3.jpg"
       [5]=>"/portrait-image-4.jpg"
    }
}

一旦我以一种真正有效的方式锁定了阵列,我想让它通过一个foreach循环,比如:

foreach ($myArray as $slide):
$image1 = $slide['0'];
$image2 = $slide['1'];
$image3 = $slide['2'];
$image4 = $slide['3'];
$image5 = $slide['4'];
$image6 = $slide['5'];
// Blah blah HTML output, so on so forth...
endforeach;

我希望我已经提供了足够的信息。提前感谢您在这里提供的任何帮助。

这是我试过的东西,似乎有点管用。。。但我收到了副本之类的。

$countH = 0;
$countV = 0;
foreach ($horizontalImages as $key => $horizImg):
    $countH++;
       foreach ($verticalImages as $k =>$vertImg):
                    $countV++;
                    if ($countH % 4 == 1):
             $variables[$countH][] = $horizImg; 
                            if ($countV % 2 == 1):
                                $variables[$countH][] = $vertImg; 
                            endif;
                    endif;
       endforeach;
    endforeach;

    <pre>
    <?php var_dump($variables); ?>
    </pre>

结果

    array(5) {
  [1]=>
  array(12) {
    [0]=>
    string(54) "/files/cache/ff89e524684c8ff50103fefd715e6f46_f473.jpg"
    [1]=>
    string(54) "/files/cache/f5e4eeec3e2e0a7fcaefdc1c756cdee8_f486.jpg"
    [2]=>
    string(54) "/files/cache/ff89e524684c8ff50103fefd715e6f46_f473.jpg"
    [3]=>
    string(54) "/files/cache/ff89e524684c8ff50103fefd715e6f46_f473.jpg"
    [4]=>
    string(54) "/files/cache/08cbd2856e4ecb8f25347540238ff356_f488.jpg"
    [5]=>
    string(54) "/files/cache/ff89e524684c8ff50103fefd715e6f46_f473.jpg"
    [6]=>
    string(54) "/files/cache/ff89e524684c8ff50103fefd715e6f46_f473.jpg"
    [7]=>
    string(54) "/files/cache/67c4cb481a31c03626b7289e66515f93_f489.jpg"
    [8]=>
    string(54) "/files/cache/ff89e524684c8ff50103fefd715e6f46_f473.jpg"
    [9]=>
    string(54) "/files/cache/ff89e524684c8ff50103fefd715e6f46_f473.jpg"
    [10]=>
    string(54) "/files/cache/046cb32b0c062a359a71e24e814d5bff_f485.jpg"
    [11]=>
    string(54) "/files/cache/ff89e524684c8ff50103fefd715e6f46_f473.jpg"
  }
  [5]=>
  array(12) {
    [0]=>
    string(54) "/files/cache/40a132f198f0648b4b9794505d8a7201_f474.jpg"
    [1]=>
    string(54) "/files/cache/f5e4eeec3e2e0a7fcaefdc1c756cdee8_f486.jpg"
    [2]=>
    string(54) "/files/cache/40a132f198f0648b4b9794505d8a7201_f474.jpg"
    [3]=>
    string(54) "/files/cache/40a132f198f0648b4b9794505d8a7201_f474.jpg"
    [4]=>
    string(54) "/files/cache/08cbd2856e4ecb8f25347540238ff356_f488.jpg"
    [5]=>
    string(54) "/files/cache/40a132f198f0648b4b9794505d8a7201_f474.jpg"
    [6]=>
    string(54) "/files/cache/40a132f198f0648b4b9794505d8a7201_f474.jpg"
    [7]=>
    string(54) "/files/cache/67c4cb481a31c03626b7289e66515f93_f489.jpg"
    [8]=>
    string(54) "/files/cache/40a132f198f0648b4b9794505d8a7201_f474.jpg"
    [9]=>
    string(54) "/files/cache/40a132f198f0648b4b9794505d8a7201_f474.jpg"
    [10]=>
    string(54) "/files/cache/046cb32b0c062a359a71e24e814d5bff_f485.jpg"
    [11]=>
    string(54) "/files/cache/40a132f198f0648b4b9794505d8a7201_f474.jpg"
  }
  [9]=>
  array(12) {
    [0]=>
    string(54) "/files/cache/c96f867163ec38d83d59350dfdb9518d_f478.jpg"
    [1]=>
    string(54) "/files/cache/f5e4eeec3e2e0a7fcaefdc1c756cdee8_f486.jpg"
    [2]=>
    string(54) "/files/cache/c96f867163ec38d83d59350dfdb9518d_f478.jpg"
    [3]=>
    string(54) "/files/cache/c96f867163ec38d83d59350dfdb9518d_f478.jpg"
    [4]=>
    string(54) "/files/cache/08cbd2856e4ecb8f25347540238ff356_f488.jpg"
    [5]=>
    string(54) "/files/cache/c96f867163ec38d83d59350dfdb9518d_f478.jpg"
    [6]=>
    string(54) "/files/cache/c96f867163ec38d83d59350dfdb9518d_f478.jpg"
    [7]=>
    string(54) "/files/cache/67c4cb481a31c03626b7289e66515f93_f489.jpg"
    [8]=>
    string(54) "/files/cache/c96f867163ec38d83d59350dfdb9518d_f478.jpg"
    [9]=>
    string(54) "/files/cache/c96f867163ec38d83d59350dfdb9518d_f478.jpg"
    [10]=>
    string(54) "/files/cache/046cb32b0c062a359a71e24e814d5bff_f485.jpg"
    [11]=>
    string(54) "/files/cache/c96f867163ec38d83d59350dfdb9518d_f478.jpg"
  }
  [13]=>
  array(12) {
    [0]=>
    string(54) "/files/cache/c09af0f66cb15621163e4018e4c34e72_f479.jpg"
    [1]=>
    string(54) "/files/cache/f5e4eeec3e2e0a7fcaefdc1c756cdee8_f486.jpg"
    [2]=>
    string(54) "/files/cache/c09af0f66cb15621163e4018e4c34e72_f479.jpg"
    [3]=>
    string(54) "/files/cache/c09af0f66cb15621163e4018e4c34e72_f479.jpg"
    [4]=>
    string(54) "/files/cache/08cbd2856e4ecb8f25347540238ff356_f488.jpg"
    [5]=>
    string(54) "/files/cache/c09af0f66cb15621163e4018e4c34e72_f479.jpg"
    [6]=>
    string(54) "/files/cache/c09af0f66cb15621163e4018e4c34e72_f479.jpg"
    [7]=>
    string(54) "/files/cache/67c4cb481a31c03626b7289e66515f93_f489.jpg"
    [8]=>
    string(54) "/files/cache/c09af0f66cb15621163e4018e4c34e72_f479.jpg"
    [9]=>
    string(54) "/files/cache/c09af0f66cb15621163e4018e4c34e72_f479.jpg"
    [10]=>
    string(54) "/files/cache/046cb32b0c062a359a71e24e814d5bff_f485.jpg"
    [11]=>
    string(54) "/files/cache/c09af0f66cb15621163e4018e4c34e72_f479.jpg"
  }
  [17]=>
  array(12) {
    [0]=>
    string(54) "/files/cache/ba56c501e8f83264894d84a53ce056d1_f467.jpg"
    [1]=>
    string(54) "/files/cache/f5e4eeec3e2e0a7fcaefdc1c756cdee8_f486.jpg"
    [2]=>
    string(54) "/files/cache/ba56c501e8f83264894d84a53ce056d1_f467.jpg"
    [3]=>
    string(54) "/files/cache/ba56c501e8f83264894d84a53ce056d1_f467.jpg"
    [4]=>
    string(54) "/files/cache/08cbd2856e4ecb8f25347540238ff356_f488.jpg"
    [5]=>
    string(54) "/files/cache/ba56c501e8f83264894d84a53ce056d1_f467.jpg"
    [6]=>
    string(54) "/files/cache/ba56c501e8f83264894d84a53ce056d1_f467.jpg"
    [7]=>
    string(54) "/files/cache/67c4cb481a31c03626b7289e66515f93_f489.jpg"
    [8]=>
    string(54) "/files/cache/ba56c501e8f83264894d84a53ce056d1_f467.jpg"
    [9]=>
    string(54) "/files/cache/ba56c501e8f83264894d84a53ce056d1_f467.jpg"
    [10]=>
    string(54) "/files/cache/046cb32b0c062a359a71e24e814d5bff_f485.jpg"
    [11]=>
    string(54) "/files/cache/ba56c501e8f83264894d84a53ce056d1_f467.jpg"
  }
}
$ch = ceil(count($horizontalImages) / 4);// Count of quartet (including incomplete tuples)
$cv = ceil(count($verticalImages) / 2);// Count of doublet (including incomplete tuples)
$c = $ch>$cv ? $ch : $cv; // Are there more - quartets or doublets?    
$out = array();
// We pass on the long side
for ($x=0;$x<$c;$x++) {
    $tmp1 = $x<$ch ? array_slice($horizontalImages, $x*4, 4) : array(); // No more quartet?
    $tmp2 = $x<$cv ? array_slice($verticalImages, $x*2, 2) : array(); // No more doublet?
    $out[] = array_merge( $tmp1, $tmp2 ); // Fold tuples together
}

http://ideone.com/2xaO2q

相关内容

  • 没有找到相关文章