Adding variables in PHP array and identifying and swapping variables

New here and completely out of my comfort zone in my current PHP course. I need help with two different things in an assignment I'm working on. The first is how to make yes appear instead of 4k or blueray in the appropriate columns in the following code:
<?php
$items = array(
array("title" => "Guardians of the Galaxy Vol. 2", 'type' => "blue-ray","price" => 19.99),
array("title" => "Wonder Woman 2017", 'type' => "4K","price" => 24.99),
array("title" => "Spider-Man: Homecoming", 'type' => "blue-ray","price" => 22.99),
array("title" => "War For The Planet Of The Apes", 'type' => "4K","price" => 19.99),
array("title" => "Baby Driver", 'type' => "blue-ray","price" => 24.99),
array("title" => "Atomic Blonde", 'type' => "4K","price" => 24.99),
array("title" => "Moana", 'type' => "blue-ray","price" => 15.99),
array("title" => "Alien: Covenant", 'type' => "blue-ray","price" => 21.96),
array("title" => "Despicable Me 3", 'type' => "4K","price" => 24.99),
array("title" => "Firefly Complete Series", 'type' => "blue-ray","price" => 20.99),
);
//sorting titles alphabetically
sort($items);
?>
<?php //creating forloop and assigning array items string values. Then looping through the title, type, and price elements to display them within the correct table rows and columns.
foreach ($items as $key => $value){
echo "<tr><td>".$value["title"]."</td><td>".$value['type']."</td><td>".$value['type']."</td><td>$".$value["price"]."</td></tr>"; }
?>
The other issue I'm having trouble figuring out is how to access and add all of the inventory values I have created in this code by multiplying $value[price] and $value [quantity] in my foreach loop:
<?php
$items = array(
array("title" => "Guardians of the Galaxy Vol. 2", 'type' => "blue-ray","price" => 19.99,"qty" => 25),
array("title" => "Wonder Woman 2017", 'type' => "4K","price" => 24.99,"qty" => 32),
array("title" => "Spider-Man: Homecoming", 'type' => "blue-ray","price" => 22.99,"qty" => 17),
array("title" => "War For The Planet Of The Apes", 'type' => "4K","price" => 19.99,"qty" => 2),
array("title" => "Baby Driver", 'type' => "blue-ray","price" => 24.99,"qty" => 25),
array("title" => "Atomic Blonde", 'type' => "4K","price" => 24.99,"qty" => 11),
array("title" => "Moana", 'type' => "blue-ray","price" => 15.99,"qty" => 21),
array("title" => "Alien: Covenant", 'type' => "blue-ray","price" => 21.96,"qty" => 19),
array("title" => "Despicable Me 3", 'type' => "4K","price" => 24.99,"qty" => 33),
array("title" => "Firefly Complete Series", 'type' => "blue-ray","price" => 20.99,"qty" => 15),
);
rsort($items);
?>
<?php //creating forloop and assigning array items string values. Then looping through the title, type, and price elements to display them within the correct table rows and columns.
//added qty column and created inventory column by multiplying qty and price values.
foreach ($items as $key => $value){
echo "<tr><td>".$value["title"]."</td><td>".$value['type']."</td><td>".$value['type']."</td><td>".$value["price"]."</td><td>".$value['qty']."</td><td>$".$value["price"]*$value['qty']."</td></tr>"; }
?>
Thank you in advanced to anyone willing to help!
Answer
Solution:
You don't have to writearray()
every time, you can just do it like this:
$var = [
["title" => "Guardians of the Galaxy Vol. 2", 'type' => "blue-ray","price" => 19.99]
];
Then for your loop and checking if something is 4k you can do this:
foreach ($items as $key => $value){
echo "<tr><td>".$value["title"]."</td><td>".(($value['type'] == '4k')? 'Yes' : $value['type'])."</td><td>$".$value["price"]."</td></tr>";
}
More information about that here: one line if statement in php
Answer
Solution:
What are your table headings? Is your expected output like this:
Title | Blue Ray | 4K | Price |
---|---|---|---|
Guardians of the Galaxy | Yes | - | $19.99 |
Wonder Woman | - | Yes | $24.99 |
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: script cache:clear returned with error code 255
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.