0

I'm trying to populate an empty array with the name and value from a foreach loop. I've created an array $variationimages = array(); and within the loop I'm saving the values by using $variationimages['name'] and $variationimages['image'].

When returning the $variationimages array I'm only getting the first item

Array
(
    [name] = Wall Profile
    [image] = <img src="example-image.jpg">
)

What I would like is for it to return something like the below and to be able to access a specific item in the array by using something like $variationimages['Roof Profile']['image'] which would return the image of the item with the name 'Roof Profile'.

 Array
    (
        [name] = Wall Profile
        [image] = <img src="example-image.jpg">
    ), 
(
            [name] = Roof Profile
            [image] = <img src="example-image.jpg">
        ),

The code I have is below:

function loop_display_variation_attribute_and_thumbnail() {
    global $product;

    // HERE your targeted product attribute taxonomy
    $taxonomy = 'pa_product_type';
    $variationimages = array();

    if( $product->is_type('variable') ) {
        foreach ( $product->get_available_variations() as $variation ) {

            if( isset($variation['attributes']['attribute_'.$taxonomy]) ) {
                // Get the "pa_product_type"
                $term_name = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name;
                $term_slug = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->slug;

                $variationimages['name'] = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name;
                $variationimages['image'] = '<img style="display:none;" class="'.$term_slug.'-image" src="' . $variation['image']['thumb_src'] .'">';
            }
        }
        echo "<div style='display:none'>"; 
                print_r($variationimages);
                echo "</div>";
    }
}

2 Answers 2

1

Shaun, just add additional [] on your variable here:

$variationimages['name'][] = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name;
$variationimages['image'][] = '<img style="display:none;" class="'.$term_slug.'-image" src="' . $variation['image']['thumb_src'] .'">';

On every iteration it will append new values.

Cheers ;-)

---EDIT after comment---

$variationimages[] = [
    'name'  => get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name,
    'image' => '<img style="display:none;" class="'.$term_slug.'-image" src="' . $variation['image']['thumb_src'] .'">'
];
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @JohnnyB1988. Thanks for your answer. Ive added the code you mentioned but it's grouping the name and image values in separate arrays. I want both the name and image in a single array like my example above
0

i think you must fill array with objects or array of arrays

like , you must create class for example Item $one_item= new Item();

and write code like that:

function loop_display_variation_attribute_and_thumbnail() {
    global $product;

    // HERE your targeted product attribute taxonomy
    $taxonomy = 'pa_product_type';
    $variationimages = array();

    if( $product->is_type('variable') ) {
        foreach ( $product->get_available_variations() as $variation ) {

            if( isset($variation['attributes']['attribute_'.$taxonomy]) ) {
                // Get the "pa_product_type"
                $term_name = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name;
                $term_slug = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->slug;
$one_item=New Item ();

                $one_item=>name = get_term_by('slug', $variation['attributes']['attribute_'.$taxonomy], $taxonomy)->name;
                $one_item=>image = '<img style="display:none;" class="'.$term_slug.'-image" src="' . $variation['image']['thumb_src'] .'">';
array_push($variationimages,$one_item);

            }
        }
        echo "<div style='display:none'>"; 
                print_r($variationimages);
                echo "</div>";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.