Reputation:
I have this array:
$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
]
];
And I am wanting to only select qty1 and or 1899.99 at times, and other times I want to access any of other qty's and or their associated prices.
Right now I am trying:
<?php foreach($items['Mac'] as $x => $x_value): ?>
<p><?= $x_value ?></p>
<?php endforeach ?>
which outputs:
1
1899.99
Above the 1 is the quantity of Mac's, and 1899.99 is the individual price, however I only want the quantity printed, not both.
How would I do this?
Upvotes: 1
Views: 169
Reputation: 136
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'];
Upvotes: 1