Associative arrays - grabbing specific keys and values
Solution 1:
Just Change your foreach
loop
This:
<?php
$items = [
'Mac' => [
'quantity' => $qty1,
'price' => 1899.99
],
'Razer Mouse' => [
'quantity' => $qty2,
'price' => 79.99
],
'WD HDD' => [
'quantity' => $qty3,
'price' => 179.99
],
'Nexus' => [
'quantity' => $qty4,
'price' => 249.99
],
'Drums' => [
'quantity' => $qty5,
'price' => 119.99
]
];
foreach($items as $key => $arrVal) {
if($key == 'Mac')
echo $key ."=". $arrVal['quantity'];
}
?>
The if(){}
controls which key
is getting printed so if you want to print
Nexus
just change the value from Mac
to Nexus
or smthing and if you want to check multiple ones just use ||
like this if($key == 'Mac' || $key == 'Nexus')
||
= OR
And if you want to get price just change $arrVal['quantity'];
to $arrVal['price'];