如何访问特定的key->值对并从PHP中的关联数组中排除特定的key->值对?

时间:2013-07-10 10:06:09

标签: php sql arrays associative-array

我有一个名为$_SESSION[BASE_SESSION_NAME]['my_cart']的关联数组。作为参考,我打印如下数组:

Array
(
    [6f993b72511c37f4ab4d7273066b8160] => Array
        (
            [pack_id] => 6f993b72511c37f4ab4d7273066b8160
            [pack_type] => test
            [pack_type_id] => 4
            [pack_name] => NEET XII Biology
            [pack_price] => 459.00
            [pack_validity] => Array
                (
                    [years] => 1
                    [months] => 0
                    [days] => 0
                )

        )

    [cart_summary] => Array
        (
            [total_no_packages] => 3
            [total_pack_price] => 1557
            [final_total_pack_price] => 1557
            [total_pack_discount] => 0
            [apply_deal_code] => 0
            [no_of_discounted_packages] => 0
        )

    [67f76ffae26ffdcde7a392c5534c972a] => Array
        (
            [pack_id] => 67f76ffae26ffdcde7a392c5534c972a
            [pack_type] => test
            [pack_type_id] => 4
            [pack_name] => NEET XII Chemistry
            [pack_price] => 599.00
            [pack_validity] => Array
                (
                    [years] => 1
                    [months] => 0
                    [days] => 0
                )

        )

    [31dc7c5d9b9cb5a2ba968e74efd4a475] => Array
        (
            [pack_id] => 31dc7c5d9b9cb5a2ba968e74efd4a475
            [pack_type] => test
            [pack_type_id] => 4
            [pack_name] => NEET XII Physics
            [pack_price] => 499.00
            [pack_validity] => Array
                (
                    [years] => 1
                    [months] => 0
                    [days] => 0
                )

        )

)

现在我只想访问具有pack_id值或值ok键pack_id的密钥。 例如,值[31dc7c5d9b9cb5a2ba968e74efd4a475]或来自键pack_id的值 为[pack_id] => 31dc7c5d9b9cb5a2ba968e74efd4a475。 但问题是还有一个数组存在密钥[cart_summary],我不想使用该数组中的任何东西。 我要对每个这样的pack_id发出查询。所以我想在循环的帮助下发出一个查询。任何人都可以帮我解决这个问题吗?提前致谢。我想要使​​用的查询如下:

SELECT package_type.package_type_name
FROM package_type
JOIN test_packages ON package_type.package_type_id = test_packages.test_pack_type_id
WHERE test_packages.test_pack_id = '$propsed_pack_id'

4 个答案:

答案 0 :(得分:5)

以下是您在评论

中要求的关联数组的foreach
$mycart = $_SESSION[BASE_SESSION_NAME]['my_cart'];// where $mycart is the array structure
                                                  //as shown above in the question
foreach($mycart as $key=>$value)
{
     //where $key holds the pack_id i.e. the array key value
     $propsed_pack_id = $value['pack_id'];
     if($key != "cart_summary")
     {
         //execute you sql query
         $query = "SELECT package_type.package_type_name
         FROM package_type
         JOIN test_packages ON package_type.package_type_id =    
         test_packages.test_pack_type_id
         WHERE test_packages.test_pack_id = '$propsed_pack_id'";
         ......

     }
 }

我希望这可以提供一些帮助。

答案 1 :(得分:1)

unset($arr["item"])删除具有" item"键的项目。来自名为$ arr。

的数组

答案 2 :(得分:1)

一种方法是你可以直接检查saran banerjee所说的'cart-summary'。

但我建议,您可以检查数组中的'pack_id'键,而不是对'cart-summary'进行硬编码检查。如果它在那里,执行该pack_id的查询,否则不执行。

$mycart = $_SESSION[BASE_SESSION_NAME]['my_cart']; 

foreach($mycart as $key=>$value)
{
     //where $key holds the pack_id 
     $propsed_pack_id = isset($value['pack_id']) ? $value['pack_id'] : '';
     if($propsed_pack_id != "")
     {
         //execute you sql query
         $query = "SELECT package_type.package_type_name
         FROM package_type
         JOIN test_packages ON package_type.package_type_id =    
         test_packages.test_pack_type_id
         WHERE test_packages.test_pack_id = '$propsed_pack_id'";

     }
 }

答案 3 :(得分:0)

如果我理解你想要什么,你应该使用一个循环并寻找正确的密钥。

如果您的后端语言是PHP。

foreach($_SESSION[BASE_SESSION_NAME]['my_cart'] as $cart) {
    if (isset($cart['pack_id'])) {
        // Here you get the pack id, only if exist as a key inside the array
    }
}

希望这有帮助...