0

I need to echo/return the data to the page like this:

Catalog: 251-2010
Gauge: 20g
Length: 10cm
Tip Size: 10mm

Here is the array called $vararray. It contains several different arrays of product variation data:

array(3) { 
    [0]=> array(1) { 
        ["251-2010"]=> array(1) { 
            ["Gauge"]=> string(3) "20g" 
        } 
    } 
    [1]=> array(1) { 
        ["251-2010"]=> array(1) { 
            ["Length"]=> string(4) "10cm" 
        } 
    } 
    [2]=> array(1) { 
        ["251-2010"]=> array(1) { 
            ["Tip Size"]=> string(4) "10mm" 
        } 
    } 
}
array(3) { 
    [0]=> array(1) { 
        ["600-VR1620"]=> array(1) { 
            ["Chart Type"]=> string(14) "Shirt" 
        } 
    } 
    [1]=> array(1) { 
        ["600-VR1152"]=> array(1) { 
            ["Chart Type"]=> string(13) "Trousers" 
        } 
    } 
    [2]=> array(1) { 
        ["600-VR16211"]=> array(1) { 
            ["Chart Type"]=> string(13) "Socks" 
        } 
    } 
}

I need something like this:

$vargroup = array();

foreach ($vararray as $vitems) {

    $varmeta = array_values($vararray);

    foreach ($varmeta as $metain => $vardetails) {
  
        vargroup[$metain]['catalog'] = $vardetails['Catalog'];
        vargroup[$metain]['gauge'] = $vardetails['Gauge'];
        vargroup[$metain]['length'] = $vardetails['Length'];
        vargroup[$metain]['tipsize'] = $vardetails['Tip Size'];

    }
    $vars_profile = '';
    foreach ($vargroup as $vgrp) {
        $vars_profile .= $vgrp[catalog] . '<br>' . $vgrp[gauge] . '<br>' . $vgrp[length] . '<br>' . $vgrp[tipsize];
    }
}
return $vars_profile;

I'm having a lot of trouble getting it right. Here is how I need it to look:

Catalog: 251-2010
Gauge: 20g
Length: 10cm
Tip Size: 10mm

Catalog: 600-VR1620
Chart Type: Shirt

Catalog: 600-VR1152
Chart Type: Trousers

Catalog: 600-VR16211
Chart Type: Socks

2
  • You shouldn't have return inside the loop. That will stop the loop after the first iteration. Commented Jan 12, 2022 at 23:09
  • Everything starting with $vars_profile = ''; shouldn't be in the first loop. Commented Jan 12, 2022 at 23:11

1 Answer 1

1

You can't get all of Catalog, Gauge, Length, and Tip Size from the same $vardetails element, they're in different elements of the array. You need to drill into each element to get its key and value.

You can create $vars_profile in the loop that's processing the original array, you don't need $vargroup.

To show the category only once, use a variable to hold the last value. Only output the category line when this field changes.

$vars_profile = '';

$last_metain = null;
foreach ($vararray as $vitem) {
    foreach ($vitem as $metain => $vardetails) {
        if ($metain != $last_metain) {
            $vars_profile .= "<p>\nCatalog: $metain<br>\n";
            $last_metain = $metain;
        }
        foreach ($vardetails as $key => $value) {
            $vars_profile .= "$key: $value<br>\n";
        }
    }
}

return $vars_profile;
Sign up to request clarification or add additional context in comments.

16 Comments

@Barmer I think this gets a step closer...thank you...but it does not produce the result I need as specified in my original question (please review again). First, the $vitems is only seeing the last item of each array. I tried using $vararray as $vkeys => $vitems instead but $vitems in a var_dump produced only the last item as well. Aside from the minor syntax issues, this code ($vars_profile = "$key: $value</br>";) does not give me the ability to assign a variable to each $key and each $value, which is needed, I think to display what I need.
Sorry, I meant to write .= for all the assignments to $vars_profile
I updated the display of the result I need to include all data from all arrays, so the logic of how I need it to work is clearer. I've been working on this for hours now, and need an epiphany!
Shouldn't the second and third VR1620 be VR1152 and VR16211?
Can you use var_export($vararray) so I can paste it into code?
|

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.