如何找到在多维$ _SESSION数组中的特定值? [英] How to find a specific value in multidimensional $_SESSION array?

查看:101
本文介绍了如何找到在多维$ _SESSION数组中的特定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 $ _ SESSION 数组来存放物品描述,数量和价格放在购物车中的物品。这一切工作的花花公子,这里的code的开始部分:

  $数量= 1;//从店铺页面中添加第一个项目
如果(使用isset($ _ POST ['add_item'])及及(使用isset($ _ SESSION ['购物车']))!){
    $ _SESSION ['购物车'] =阵列();
    $ _SESSION ['购物车'] [$ _ POST ['PRODUCT_DESCRIPTION'] =阵列('量'= GT; $数量,'价格'=> $ _ POST ['PRODUCT_PRICE']);
    标题(位置:http://website.com/cart.php);
    }

这将加载相应的信息在 $ _ SESSION ['购物车'] 阵列,当我的foreach 循环播放在车页面,我得到我的购物车的确切内容。根据需要所有这一切工作的。

所以现在我想要做的是搜索 $ _ SESSION ['购物车'] 数组来确定特定产品在 $ _SESSION ['购物车'] 阵列,如iPhone外壳 - 黑色。

我已经尝试了各种形式的的回声$ _SESSION ['购物车'] ['PRODUCT_DESCRIPTION'] 键,什么也没得到。当我的print_r 数组,我得到这样的:

 阵列

    [iPhone案例 - 黑色] =>排列
        (
            [数量] => 1
            [价格] => 9.49
        ))

这是正确的。执行的var_dump 产生预期的结果是:

 阵列(1){
  [iPhone案例 - 黑色] =>
  阵列(2){
    [数量] =>
    INT(1)
    [价格] =>
    串(4)9.49
  }

}

当然,如果有更好的方法来构建数组,我接受它。

$ _ SESSION ['购物车'] 数组和服务于适当的$ C $ -

所有我想要做的是找出是否黑色iPhone案基于C的结果。

我曾尝试如果(in_array('iPhone外壳 - 黑',$ _SESSION ['购物车']))这将产生什么,当我展开到如果(in_array('iPhone外壳 - 黑',$ _SESSION ['购物车'] [0])) ... [1] ... ['PRODUCT_DESCRIPTION'] 我收到错误消息:

警告:in_array()预计参数2是阵列,在...空给出

我在想什么,将只是让我检查 $ _ SESSION ['购物车'] 数组找到价值,iPhone外壳 - 黑?是我唯一的选择一个的foreach 循环?

许多诚挚的感谢提前!

更新
code张贴根据来自@rmmoul要求:

  //从店铺页面中添加第一个项目
如果(使用isset($ _ POST ['add_item'])及及(使用isset($ _ SESSION ['购物车']))!){
$ _SESSION ['购物车'] [] =阵列('PRODUCT_DESCRIPTION'=> $ _ POST ['PRODUCT_DESCRIPTION'],'量'=> $数量,'价格'=> $ _ POST ['PRODUCT_PRICE']);
标题(位置:http://website.com/cart.php);
}

返回此通过的print_r

 阵列

    [0] =>排列
        (
            [PRODUCT_DESCRIPTION] => iPhone保护壳 - 黑色
            [数量] => 1
            [价格] => 9.49
        )    [1] =>排列
        (
        [PRODUCT_DESCRIPTION] => iPhone保护壳 - 黑色
        [数量] => 1
        [价格] => 9.49
    ))


解决方案

由于您使用的是 $ _ POST ['PRODUCT_DESCRIPTION'] 为重点分配的值将成为关键,即iPhone外壳 - 黑

试试这个:

 如果(使用isset($ _ SESSION ['购物车'] ['iPhone外壳 - 黑色'])){的foreach($ _ SESSION ['购物车'] ['iPhone外壳 - 黑']为$关键=> $值){回声$键&LT。$价值。; BR>中;}

I am using a $_SESSION array to store item description, quantity and price for items placed in a shopping cart. This all works dandy, here's the beginning portion of the code:

$quantity = 1;

// add first item from shop page
if(isset($_POST['add_item']) && (!isset($_SESSION['cart']))) {
    $_SESSION['cart'] = array();
    $_SESSION['cart'][$_POST['product_description']] = array('quantity' => $quantity, 'price' => $_POST['product_price']);
    header("Location: http://website.com/cart.php");
    }

This loads the $_SESSION['cart'] array with the appropriate information and when I foreach loop it in the cart page, I get the exact contents of my cart. All of this works as desired.

So now what I want to do is search the $_SESSION['cart'] array to determine if a specific item is in the $_SESSION['cart'] array, such as "iPhone case - Black".

I have tried various forms of echo $_SESSION['cart']['product_description'] and get nothing. When I print_r the array, I get this:

Array
(
    [iPhone case - Black] => Array
        (
            [quantity] => 1
            [price] => 9.49
        )

)

Which is correct. Performing a var_dump yields the expected result:

array(1) {
  ["iPhone case - Black"]=>
  array(2) {
    ["quantity"]=>
    int(1)
    ["price"]=>
    string(4) "9.49"
  }

}

Certainly, if there is a better way to construct the array, I'm open to it.

All I want to do is find out if "iPhone case - Black" is in the $_SESSION['cart'] array and serve up the appropriate code based on the result.

I have tried if (in_array('iPhone case - Black', $_SESSION['cart'])) which yields nothing, and when I expand that to if (in_array('iPhone case - Black', $_SESSION['cart'][0])) or ...[1] or ...['product_description'] I receive the error:

Warning: in_array() expects parameter 2 to be array, null given in...

What am I missing that would simply let me check the $_SESSION['cart'] array to find the value, "iPhone case - Black"? Is my only option a foreach loop?

Many sincere thanks in advance!

UPDATE Code posting as per request from @rmmoul:

// add first item from shop page
if(isset($_POST['add_item']) && (!isset($_SESSION['cart']))) {
$_SESSION['cart'][] = array('product_description'=> $_POST['product_description'], 'quantity' => $quantity, 'price' => $_POST['product_price']);
header("Location: http://website.com/cart.php");
}

Returns this through a print_r:

Array
(
    [0] => Array
        (
            [product_description] => iPhone case - Black
            [quantity] => 1
            [price] => 9.49
        )

    [1] => Array
        (
        [product_description] => iPhone case - Black
        [quantity] => 1
        [price] => 9.49
    )

)

解决方案

since you are using $_POST['product_description'] as key the assigned value will become key i.e. 'iPhone case - Black'.

try this:

 if (isset($_SESSION['cart']['iPhone case - Black'])){

foreach($_SESSION['cart']['iPhone case - Black'] as $key=>$value){

echo $key."  ".$value."<br>";

}

这篇关于如何找到在多维$ _SESSION数组中的特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆