具有未知键的多维数组

I'm having a multidimensional array from which I don't know the keys and I need all key's with their value.

My array is database filled:

$rows[$product_id][$productgroup_id] = $amount

So the array is for example filled with 2 products:

$rows[108][3] = 2
$rows[2][5] = 4

So my array now holds 2 product:

  • Product_id 108 from productgroup 3 with an amount of 2
  • Product_id 2 from productgroup 5 with an amount of 4

Now I need to walk through the array and I need the keys and the amount. So I'm thinking in a foreach loop

foreach($rows as $row){
  foreach($row as $key => $value){
    echo "Key:".$key." Value: ".$value."<br>";"
  }
}

But this only echo's the first key, product_id and the amount. But I need the product_id, productgroup_id and the amount. So how do I also get the productgroup_id?

the code you have so far is almost there, you simply need to extract both id's with the foreach loop.

foreach($rows as $product_id => $group){
  foreach($group as $productgroup_id => $value){
    echo "Product ID:" . $product_id . " Group:".$productgroup_id." Value: ".$value."<br>";"
  }
}

If you want to see/debug the array, you could use the php function print_r(), in your case, it will be echo print_r($row).