How to use more than one variable in a group array?

Hi all.

I am a PHP novice and am currently learning about group arrays. I have the following code and have used such examples often and they work great, but I’ve never tried to do anything more complex with this sort of thing. I’m just wondering how I can add more variables to my result output.

Here is the working code I have:

$check_alt_sizes = mysqli_query($link, "SELECT model, version, size, category FROM items WHERE model = '$model' AND size != '$category' AND id != '$item_id'");
  if (mysqli_num_rows($check_alt_sizes) >= 1) {
             $group = array();
             while ($row = mysqli_fetch_assoc($check_alt_sizes)) {
                 $group[ $row['category'] ][] = $row;
         }

then later for the output:

     foreach ($group as $sizes => $alt_size_urls) {
             foreach ($alt_size_urls as $alt__size_url) {
                 echo "<a href='/items/"; echo "$alt_size_url[slug]"; // slug set elsewhere
                 echo "'>";
                 echo "$sizes</a>";
             }
         }
 }

Now the $sizes part displays a list of sizes that I have gotten from grabbing $row[‘category’] in the initial $group from the query. What I would like to know is, how can I add more variables to this group for output…

Currently it displays as:

Alternate Sizes:
size1
size2

But I would like to be able to add version (that I am also pulling from the same mysqli query) as well, to allow output such as:

Alternate Sizes:
Version1 - size1
Version2 - size1
Version3 - size2

I tried doing this:

echo $row['version']; echo "$sizes</a></p></li>";

But that just uses the first version found and applies it to every item. Obviously because $sizes is looping and the version echo is not.

How do I go about doing this?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.