0

我有这个数组:

$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 ) 

                    ) 

            ) 

    )

我想foreach为每个filenameand做一个循环imagedescription,所以我可以在我的应用程序中使用filenameandimagedescription作为<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; ?>
4

2 回答 2

1

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

$results = array();
foreach($bigArray as $group){
  foreach($group['imageinfo']['filename'] as $index => $fileName){
    $results[] = array(
      'file' => $filename,
      'description' =>$group['imageinfo']['imagedescription'][$index]
    );
  }
}
于 2013-02-19T14:20:30.167 回答
1

您可以简单地相互嵌套两个循环来实现这一点

foreach ($array as $item) {
    foreach ($item as $key => $subitem) {
         //do something with subitem
    }
}
于 2013-02-19T14:09:59.187 回答