doujiao3998 2016-09-24 17:47
浏览 42
已采纳

搜索2个数组并比较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" } 
  • 写回答

1条回答 默认 最新

  • douzhouqin6223 2016-09-24 18:37
    关注

    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 />';
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧