2

I have 3 arrays

$description[$cat_id][$kit_id][$item_id]  = $description ;
$description2[$cat_id][$kit_id][$item_id] = $description2 ;
$qty_total[$cat_id][$kit_id][$item_id]    = $qty;

foreach ($qty_total AS $key => $val) {
    echo "Cat: ".$key."<br />";
    foreach ($val AS $kkey => $kval) {
        echo "&nbsp;&nbsp;Kit: ".$kkey."<br />";
        foreach ($kval AS $ikey => $ival) {
            echo "&nbsp;&nbsp;&nbsp;&nbsp;Item: ".$ikey." - ".$ival."<br />";      
        }
    }
}

This brings only qty.

My question is how to get qty, description, description2 for given [cat_id][kit_id][item_id]?

2
  • 1
    Why are you putting them in separate arrays? Why not $items[$cat_id][$kit_id][$item_id] = ["description" => $description, "description2" => $description2, "qty" => $qty]; Commented Mar 12, 2020 at 11:52
  • Also, it doesn't make sense for the array variable to be the same as the variable that you're using to set an element. Commented Mar 12, 2020 at 11:53

1 Answer 1

1

You need to use keys of all foreach() loop to access other variables value

foreach ($qty_total AS $key => $val) {
    foreach ($val AS $kkey => $kval) {
        foreach ($kval AS $ikey => $ival) {
            echo $ival;
            echo PHP_EOL;
            echo $description2[$key][$kkey][$ikey];
            echo PHP_EOL;
            echo $description[$key][$kkey][$ikey];
        }
    }
}

Sample output: https://3v4l.org/9uYfT

Sign up to request clarification or add additional context in comments.

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.