【问题标题】:PHP Compare 2 Arrays and display items in bothPHP比较2个数组并在两者中显示项目
【发布时间】:2017-07-02 14:46:07
【问题描述】:

我正在做一个大学作业,我已经看了几个小时的代码,我迷路了:(

我需要一个会话数组来存储购物车物品,然后显示另一个数组中的完整物品

数据数组

$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',
        ),

会话数组

$_SESSION['cart'][0];

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

显示代码

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>";
        } 
}

购物车输出

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

【问题讨论】:

  • 您在这里遇到什么问题?同时向我们展示echo "&lt;pre/&gt;";print_r($_SESSION['cart']);的值
  • 您使用$cartlist 就好像它是包含数据的数组,但您应该改用$pumps[$cartlist][..],因为$cartlist 只包含一个键查找

标签: php multidimensional-array foreach


【解决方案1】:

我的假设和正确的都在下面给出:-

我假设echo "&lt;pre/&gt;";print_r($_SESSION['cart']) 如下所示:-

Array ( 
  0 => 'GB96CSUN', 
  1 => 'GP792', 
  2 => 'TK39TLSIG' 
);

基于此代码需要:-

<?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(我已经给出了我的本地文件图像的路径)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    相关资源
    最近更新 更多