循环通过多维数组

时间:2013-02-19 14:06:54

标签: php

我有这个数组:

$products = Array ( 

    [0] => Array ( 

        [keyseries_id] => 7 
        [product_id] => 1 
        [name] => Defender standaard sleutel 
        [price] => 6.25 
        [description] => Dit is een test voor de product descriptions 
        [image_id] => 1 
        [imageinfo] => Array ( 

                [filename] => Array ( 

                        [0] => defender_thumb.jpg 
                        [1] => defender_standaard.jpg ) 

                [imagedescription] => Array ( 

                        [0] => dit is een testafbeelding 
                        [1] => Dit is een 2e testafbeelding ) 

                    ) 

            ) 

    [1] => Array ( 

        [keyseries_id] => 7 
        [product_id] => 2 
        [name] => Defender Klapsleutel 
        [price] => 7.00 
        [description] => Dit is de defender klapsleutel.. NU IN DE AANBIEDING!!! 
        [image_id] => 3 
        [imageinfo] => Array ( 

                [filename] => Array ( 

                        [0] => defender_thumb.jpg 
                        [1] => defender_klap.jpg ) 

                [imagedescription] => Array ( 

                        [0] => Defender slot 
                        [1] => Defender klapsleutel ) 

                    ) 

            ) 

    )

我想为每个foreachfilename执行imagedescription循环,因此我可以将filenameimagedescription用于<img>我的应用程序中的html标记。有人可以告诉我该怎么做吗?

+++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++

答案:感谢菲利普

像这样实现了foreach循环:

<?php foreach($product['imageinfo']['filename'] as $index => $filename): ?>

                        <?php echo '<img src="http://localhost/sleutelservice/uploads/images/' . $filename . '" alt="' . $product['imageinfo']['imagedescription'][$index].  '">';  ?>

                    <?php endforeach; ?>

2 个答案:

答案 0 :(得分:1)

您可以简单地将两个循环嵌套在一起以实现此目的

foreach ($array as $item) {
    foreach ($item as $key => $subitem) {
         //do something with subitem
    }
}

答案 1 :(得分:1)

我假设您对每个文件都有描述:

$results = array();
foreach($bigArray as $group){
  foreach($group['imageinfo']['filename'] as $index => $fileName){
    $results[] = array(
      'file' => $filename,
      'description' =>$group['imageinfo']['imagedescription'][$index]
    );
  }
}