从2个php多维数组中提取值同时循环

时间:2016-07-16 18:37:41

标签: php arrays multidimensional-array

我使用 2个字符串数组,其中一个我有完整的尺寸图片网址,其他人有缩略图图片网址

我正在使用<img>建立for loop标记,但我不知道如何从中访问第二个数组值。我试着用这个做一个例子:

$array_images[70] = array("image1", "image2", "image3");        
$array_thumbs[70] = array("thumb1", "thumb2", "thumb3");        

print_r($array_images);
print_r($array_thumbs);


foreach( $array_images as $key => $value ){
    foreach( $value as $keyv => $valuev ){
        echo $valuev."\n";
    }
}

这是输出:

Array
(
    [70] => Array
        (
            [0] => image1
            [1] => image2
            [2] => image3
        )

)
Array
(
    [70] => Array
        (
            [0] => thumb1
            [1] => thumb2
            [2] => thumb3
        )

)
image1
image2
image3

我想要show image1-&gt; thumb1。但我不知道如何在for循环中合并两个数组,真的尝试了一些循环,但我还没有解决它。有什么想法吗?

3 个答案:

答案 0 :(得分:1)

由于两个数组的长度相等并且使用数字索引(其中一个数组的每个索引的值对应于另一个的数据:并行数组),因此您可以安全地使用一个两个循环...然后使用$key访问另一个。下面的代码片段足以说明这个想法...

    <?php
        // SIMULATING SUB-ARRAYS FOR FULL-SIZE IMAGES...
        $array_images1      = array("image1", "image2", "image3");
        $array_images2      = array("image4", "image5", "image6");

        // SIMULATING SUB-ARRAYS FOR THUMBNAIL IMAGES...
        $array_thumbs1      = array("thumb1", "thumb2", "thumb3");
        $array_thumbs2      = array("thumb4", "thumb5", "thumb6");

        // SIMULATING MULTIDIMENSIONAL ARRAY OF FULL/THUMB IMAGES...
        $array_main_full    = array($array_images1, $array_images2);  //<== SAME KEY/SAME IMAGE ONLY FULL.
        $array_thumbs_full  = array($array_thumbs1, $array_thumbs2);;  //<== SAME KEY/SAME IMAGE ONLY THUMB.

        // ONE LOOP: INTENDED RESULT
        foreach($array_main_full as $iKey=>$array_images){
            foreach( $array_images as $key => $value ){
                echo $value . "<br />";
                echo $array_thumbs_full[$iKey][$key] . "<br /><br />";
            }

        }

        // PRODUCES::           
        string 'image1' (length=6)
        string 'thumb1' (length=6)

        string 'image2' (length=6)
        string 'thumb2' (length=6)

        string 'image3' (length=6)
        string 'thumb3' (length=6)

        string 'image4' (length=6)
        string 'thumb4' (length=6)

        string 'image5' (length=6)
        string 'thumb5' (length=6)

        string 'image6' (length=6)
        string 'thumb6' (length=6)

确认HERE

答案 1 :(得分:1)

此代码&#39;加入&#39; 2个阵列在一起:

$array = array();
array_walk(array_combine($images, $thumbs), function($v, $k) use(&$array){
    $array[] = array(
        'image' => $k,
        'thumb' => $v
    );
});

print_r($array);将输出:

Array
(
    [0] => Array
        (
            [image] => image1
            [thumb] => thumb1
        )

    [1] => Array
        (
            [image] => image2
            [thumb] => thumb2
        )

    [2] => Array
        (
            [image] => image3
            [thumb] => thumb3
        )

)

请注意,我使用了名为imagesthumbs的变量。请参阅操作中的代码here

如果你想循环新数组,请使用以下内容:

foreach($array as $v){
    echo 'Thumb: ' . $v['thumb'] . '<br>';
    echo 'Image: ' . $v['image'] . '<br><br>';
}

输出:

Thumb: thumb1
Image: image1

Thumb: thumb2
Image: image2

Thumb: thumb3
Image: image3

在行动here中查看。

答案 2 :(得分:0)

您可以使用array_combine

$images = array_combine($array_images,$array_thumbs);