简体   繁体   English

在foreach循环中嵌套值

[英]Nesting values in a foreach loop

I have a series of loops that and I need to get the values to pass correctly. 我有一系列循环,我需要获取值才能正确传递。 Basically I have a product object from hikashop in joomla. 基本上我有一个来自joomla的hikashop的产品对象。 In order to get my display to work the way I want it to behave, I had to separate the images into a separate array and use array_unique to filter duplicate images as hikashop assigns the default to each product variant and was getting duplicate images for each variant on the display. 为了使我的显示器按我希望的方式工作,我不得不将图像分离到一个单独的数组中,并使用array_unique过滤重复的图像,因为hikashop将默认值分配给每个产品变体,并获取每个变体的重复图像在显示器上。 I need the display to show each filtered image, but then assign the $char_id to them image so that when it is clicked on it will change the main product image. 我需要在显示屏上显示每个过滤的图像,然后将$char_id分配给它们图像,以便在单击它时将更改主要产品图像。

So wher I'm at is that The images are displaying correctly, but it is only picking up the first key in $characteristic->variant_characteristic_id for every image rather than getting the unique key for each image. 因此,我想知道的是图像是否正确显示,但是它只是为每个图像选择$characteristic->variant_characteristic_id的第一个键,而不是为每个图像获取唯一键。

I fear I'm a bit over my head here, Thanks 我担心我在这里有点头疼,谢谢

Here is my code block: 这是我的代码块:

if ($this->element->variants):
    $i = 0;
    foreach($this->element->variants as $variant):
        foreach($variant->characteristics as $k => $characteristic):
            $char_id = $characteristic->variant_characteristic_id;
            $cat_id = $k;
            $char_name = $characteristic->characteristic_value;
            foreach($variant->images as $key => $image):
                $images[$i] = array($key => $image);
                $i++;
            endforeach;
            $images = array_map("unserialize", array_unique(array_map("serialize", $images)));
        endforeach;
    endforeach;
    echo '<pre>'.print_r($images).'</pre>';
    foreach($images as $image):
        echo '
            <div class="product-thmb-group">
                <img id="hikashop_child_image_'.$char_id.'" class="hikashop_child_image" src="' . $this->image->uploadFolder_url . $image[0]->file_path . '" alt="hikashop_child_image_' . $char_id . '"  />
                <span class="product-thmb-title">'.$char_name.'</span>
            </div>';
    endforeach;
endif;

it would help if you also provided an example payload, payload being whatever is in $this->element->variants. 如果您还提供了示例有效负载,这将有所帮助,有效负载就是$ this-> element-> variants中的任何内容。

Anyway spotting the problem was easy, you are overwriting the values of $char_id, $cat_id, $char_name on each iteration of the loop. 无论如何发现问题都很容易,您在循环的每次迭代中都会覆盖$char_id, $cat_id, $char_name的值。

You are also iterating through all of the images once for each item in $variant->characteristics which is probably not what you are intending. 您还需要为$variant->characteristics每个项目遍历所有图像一次,这可能不是您想要的。 Without seeing the payload that you are trying to parse I can only guess but I think what you want to do is something along the line of: 在没有看到您要解析的有效载荷的情况下,我只能猜测,但是我认为您想要做的事情类似于:

$i = 0;
$images = [];
foreach($this->element->variants as $variant) {
    $characteristic = $variant->characteristics[$i];
    $image = $variant->images[$i];

    $images[$image] = [
        "id" => $characteristic->variant_characteristic_id,
        "name" => $characteristic->characteristic_value,
        "image" => $image, // this is optional
    ];
    $i++;
}

Then you should be able to loop through the array 然后,您应该可以遍历数组

foreach($images as $image => $meta) {
    echo "Image src: ", $image,
         " Id: ", $meta['id'], " Name: ", $meta['name'], "\n";
}

Or if you kept the optional assignment above 或者,如果您保留了上面的可选作业

foreach($images as $data) {
    echo "Image src: ", $data['image'],
         " Id: ", $data['id'], " Name: ", $data['name'], "\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM