如果键值对存在于多维数组中. [英] If Key Value pair exists in multidimensional array.. How to?

查看:91
本文介绍了如果键值对存在于多维数组中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个购物车,它的购物车"数组如下:

I have a codeigniter shopping cart going and its "cart" array is the following:

Array (
[a87ff679a2f3e71d9181a67b7542122c] => Array
    (
        [rowid] => a87ff679a2f3e71d9181a67b7542122c
        [id] => 4
        [qty] => 1
        [price] => 12.95
        [name] => Maroon Choir Stole
        [image] => 2353463627maroon_3.jpg
        [custprod] => 0
        [subtotal] => 12.95
    )

[8f14e45fceea167a5a36dedd4bea2543] => Array
    (
        [rowid] => 8f14e45fceea167a5a36dedd4bea2543
        [id] => 7
        [qty] => 1
        [price] => 12.95
        [name] => Shiny Red Choir Stole
        [image] => 2899638984red_vstole_1.jpg
        [custprod] => 0
        [subtotal] => 12.95
    )

[eccbc87e4b5ce2fe28308fd9f2a7baf3] => Array
    (
        [rowid] => eccbc87e4b5ce2fe28308fd9f2a7baf3
        [id] => 3
        [qty] => 1
        [price] => 14.95
        [name] => Royal Blue Choir Stole
        [image] => 1270984005royal_vstole.jpg
        [custprod] => 1
        [subtotal] => 14.95
    )

)

我的目标是遍历此多维数组,了解如何以及是否存在具有键值对"custprod == 1"的任何产品,然后我的结帐页面将显示一件事,如果购物车中没有自定义产品,显示另一件事.任何帮助表示赞赏.谢谢.

My goal is to loop through this multidimensional array some how and if ANY product with the key value pair "custprod == 1" exists, then my checkout page will display one thing, and if no custom products are in the cart it displays another thing. Any help is appreciated. Thanks.

推荐答案

您可以使用 array_key_exists .或简单地检查一下arr['custprod'] isset (两个函数均处理null不同).

Rather than looping over it, you can check for the custprod key using array_key_exists. Or simply check to see if arr['custprod'] isset (both functions handle null differently).

$key = "custprod";
$arr = Array(
    "custprod" => 1, 
    "someprop" => 23
);
if (array_key_exists($key, $arr) && 1 == $arr[$key]) {
    // 'custprod' exists and is 1
}

这篇关于如果键值对存在于多维数组中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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