Will Barangi Will Barangi - 2 months ago 9
PHP Question

Searching Through 2 Arrays and Comparing the results PHP

I have created two arrays, one contains an inventory and the other contains orders. The inventory has a 2 dimensional array that includes the title of the book the format of the book and the price...The second array contains the title of the book and the format...I need to somehow loop through the inventory and match it with the orders..so if I have an order of Just Mercy in Softcover format, I need to go to inventory and pull out the price of Just Mercy, softcover format...
I know how to do loops but I have no idea how to compare..
Here is the array that contains the books..

$_SESSION["inventory"];


which prints out something like

Array ( [0] => Array ( [title] => The Boys in the Boat [author] => Daniel James Brown [isbn] => 067002581X [hardcover] => 19.99 [hc-quantity] => 5 [softcover] => 16.99 [sc-quantity] => 9 [e-book] => 16.99 ) [1] => Array ( [title] => Harry Potter and the Cursed Child [author] => J. K. Rowling, Jack Thorne, John Tiffany [isbn] => 1338099133 [hardcover] => 18.95 [hc-quantity] => 25 [softcover] => 17.98 [sc-quantity] => [e-book] => 0 ) [2] => Array ( [title] => Just Mercy [author] => Bryan Stevenson [isbn] => 0812994520 [hardcover] => 17.50 [hc-quantity] => 8 [softcover] => 16.25 [sc-quantity] => 10 [e-book] => 16.25 ) [3] => Array ( [title] => Me Before You [author] => Jojo Moyes [isbn] => 0670026603 [hardcover] => 18.95 [hc-quantity] => 2 [softcover] => 17.50 [sc-quantity] => 1 [e-book] => 17.25 ) [4] => Array ( [title] => A Thousand Splendid Suns [author] => Khaled Hosseini [isbn] => 1594489505 [hardcover] => 19.00 [hc-quantity] => 7 [softcover] => 15.50 [sc-quantity] => 4 [e-book] => 14.95 ) [5] => Array ( [title] => The Wright Brothers [author] => David McCullough [isbn] => 1476728742 [hardcover] => 21.95 [hc-quantity] => 3 [softcover] => 18.95 [sc-quantity] => 3 [e-book] => 18.95 ) )


and here is the other array that contains orders;

$value=$_POST['orders'];


which prints something like

Array ( [Harry Potter and the Cursed Child] => hardcover [Just Mercy] => softcover [Me Before You] => e-book ) Array ( [0] => Harry Potter and the Cursed Child [1] => Just Mercy [2] => Me Before You ) Array ( [0] => hardcover [1] => softcover [2] => e-book )


How do I loop through the inventory and extract out the price...I honestly have no Idea on how to do it..thank you
To answer a question when I use vardump on post I get the following

array(3) { ["Harry Potter and the Cursed Child"]=> string(9) "hardcover" ["Just Mercy"]=> string(9) "hardcover" ["Me Before You"]=> string(9) "hardcover" }

Answer

Assuming the fact that $_SESSION["inventory"] contains the entire inventory of books and $_POST['orders'] contains the orders, the solution would be like this:

foreach($_SESSION["inventory"] as $bookDetails){
    foreach($_POST['orders'] as $k => $v){
        if($bookDetails['title'] == $k){
            // $bookDetails[$v] will give you the price of the particular book
            echo 'The price of ' . $k . ' is ' . $bookDetails[$v] . '<br />';
        }
    }
}