我创建了两个数组,一个包含清单,另一个包含订单。库存有一个二维数组,包括书的标题,书的格式和价格......第二个数组包含书的标题和格式......我需要以某种方式循环通过库存和匹配它与订单..如果我有Softcover格式的Just Mercy订单,我需要去库存并拿出Just Mercy,softcover格式的价格...... 我知道如何做循环,但我不知道如何比较.. 这是包含书籍的数组..
$_SESSION["inventory"];
打印出类似
的内容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 ) )
这是包含订单的另一个数组;
$value=$_POST['orders'];
打印类似
的内容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 )
如何循环浏览库存并提取价格......老实说我对如何做到这一点没有任何想法..谢谢你 要在帖子上使用vardump时回答问题,我会得到以下内容
array(3) { ["Harry Potter and the Cursed Child"]=> string(9) "hardcover" ["Just Mercy"]=> string(9) "hardcover" ["Me Before You"]=> string(9) "hardcover" }
答案 0 :(得分:1)
假设$_SESSION["inventory"]
包含整个图书清单且$_POST['orders']
包含订单,解决方案将是这样的:
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 />';
}
}
}