首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP比较两个数组并在两者中显示项

PHP比较两个数组并在两者中显示项
EN

Stack Overflow用户
提问于 2017-02-14 18:41:40
回答 1查看 53关注 0票数 2

我正在做一项uni任务,现在我已经看了几个小时的代码,我迷路了:

我需要获取一个存储购物车商品的会话数组,然后显示另一个数组中的全部商品

数据数组

代码语言:javascript
复制
$pumps = array(
    'GB96CSUN' => array(
        'img' => 'images/GB96CSUN.gif',
        'title' => '1938 Gilbert & Barker Model 96C Sunray',
        'desc' => 'Beautiful authentic yellow and orange paint scheme highlight this Sunray Gilbert & Barker early electric gas pump. Completely restored inside and out. Correct Gilbarco nozzle. Museum quality. (Rolling Stand Not Included)',
        'price' => '$5495 ',
        ),

    'TK39TLSIG' => array(
        'img' => 'images/TK39TLSIG.gif',
        'title' => '1939 Tokheim Model 39 (Tall) Signal Gasoline',
        'desc' => 'Impressive size and paint scheme, this Tokheim 39 Tall Signal Gasoline pump signaled that the end of the pre-war, "tall" pump era was coming to a close. This magnificent example with its vintage gas brand is near mint. Completely restored. Correct Tokheim nozzle. Near mint. (Rolling Stand Not Included)',
        'price' => '$6495',
        ),

会话数组

代码语言:javascript
复制
$_SESSION['cart'][0];

Array ( [0] => GP8002 [1] => GP792 [2] => RGP300A ) 

显示代码

代码语言:javascript
复制
foreach ($_SESSION['cart'] as $cartlist){
    if (in_array($cartlist, $pumps)){
        echo "<div class='container'>";
            echo    "<nav>";
                echo"<a href='#'><img src='images/$pid.jpg' height='250px'></a>";
            echo"</nav>";
            echo "<article>";
                echo  "<h1>{$cartlist['title']}</h1>";
                echo "<p>{$cartlist['desc']}</p>";
                echo "<p> Price :{$cartlist['price']</p>";
            echo "</article>";
        echo "</div>";
        } 
}

购物车输出

代码语言:javascript
复制
Array
(
    [0] => GP8002
    [1] => GP792
    [2] => RGP300A
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-14 18:57:40

我假设的和正确的都给出了如下:

我假设echo "<pre/>";print_r($_SESSION['cart'])如下所示:

代码语言:javascript
复制
Array ( 
  0 => 'GB96CSUN', 
  1 => 'GP792', 
  2 => 'TK39TLSIG' 
);

基于此代码需要:-

代码语言:javascript
复制
<?php
error_reporting(E_ALL); // check all type of errors
ini_set('display_errors',1); // display those errors
session_start(); // start session to use session data
$pumps = array(
'GB96CSUN' => array(
    'img' => 'images/GB96CSUN.gif',
    'title' => '1938 Gilbert & Barker Model 96C Sunray',
    'desc' => 'Beautiful authentic yellow and orange paint scheme highlight this Sunray Gilbert & Barker early electric gas pump. Completely restored inside and out. Correct Gilbarco nozzle. Museum quality. (Rolling Stand Not Included)',
    'price' => '$5495 '
    ),
'TK39TLSIG' => array(
    'img' => 'images/TK39TLSIG.gif',
    'title' => '1939 Tokheim Model 39 (Tall) Signal Gasoline',
    'desc' => 'Impressive size and paint scheme, this Tokheim 39 Tall Signal Gasoline pump signaled that the end of the pre-war, "tall" pump era was coming to a close. This magnificent example with its vintage gas brand is near mint. Completely restored. Correct Tokheim nozzle. Near mint. (Rolling Stand Not Included)',
    'price' => '$6495'
    )
);

// comment this below line while using the code   
$_SESSION['cart'] = Array ( 0 => 'GB96CSUN', 1 => 'GP792', 2 => 'TK39TLSIG' ); // comment this line while using code

foreach ($_SESSION['cart'] as $cartlist){
    if (in_array($cartlist, array_keys($pumps))){ // check here i used array_keys()?>
       <div class='container'>
           <nav>
              <a href='#'><img src="<?php echo $pumps[$cartlist]['img'];?>" height='250px'></a>
           </nav>
           <article>
              <h1><?php echo $pumps[$cartlist]['title'];?></h1>
               <p><?php echo $pumps[$cartlist]['desc'];?></p>
               <p> Price :<?php echo $pumps[$cartlist]['price'];?></p>
           </article>
       </div>
<?php } }?>

本地屏幕的输出:- http://i.share.pho.to/bbd07738_o.png (我已经给出了本地文件镜像的路径)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42223992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档