通过['value']从数组中获取数据

时间:2013-11-11 03:29:57

标签: php arrays

在下面的数组中,提取我想要的数据['rowid']的最佳方法是什么。我尝试了类似的东西,但似乎无法得到它。

阵列:

array(4) { 
    ["b5cfec3e70d0d57ea848d5d8b9f14d61"]=> array(7) { 
        ["rowid"]=> string(32) "b5cfec3e70d0d57ea848d5d8b9f14d61" 
        ["id"]=> string(3) "232" 
        ["qty"]=> string(1) "1" 
        ["price"]=> string(2) "15" 
        ["name"]=> string(13) " DVD" 
        ["options"]=> array(4) { 
            ["description"]=> string(43) " retail DVD for personal use only." 
            ["image"]=> string(36) "d31e3bc3e820b7faef50a400f721125a.jpg" 
            ["additional_info"]=> NULL 
            ["attributes"]=> string(29) "a:1:{s:6:"Format";s:3:"PAL";}" 
        } 
        ["subtotal"]=> int(15) 
    } 
    ["eda80a3d5b344bc40f3bc04f65b7a357"]=> array(7) { 
        ["rowid"]=> string(32) "eda80a3d5b344bc40f3bc04f65b7a357" 
        ["id"]=> string(3) "267" 
        ["qty"]=> string(1) "1" 
        ["price"]=> string(4) "9.99" 
        ["name"]=> string(3) "DVD" 
        ["options"]=> array(4) { 
            ["description"]=> string(0) "" 
            ["image"]=> string(0) "" 
            ["additional_info"]=> NULL 
            ["attributes"]=> NULL 
        } 
        ["subtotal"]=> float(9.99) 
    } 
    ["total_items"]=> int(2) 
    ["cart_total"]=> float(24.99) 
}

3 个答案:

答案 0 :(得分:0)

由于您的数组包含您不需要迭代的值,因此创建一个包含要跳过的键的数组,然后您可以成功地每次迭代选择行ID:

<?php

// array keys to skip
$skip = array( 'total_items', 'cart_total' );

foreach($yourarray as $key => $row) {

    if(in_array($key, $skip))
        continue; // skip values we don't need in this array

    echo $row['rowid'] . "\n";

}

// output:
// b5cfec3e70d0d57ea848d5d8b9f14d61
// eda80a3d5b344bc40f3bc04f65b7a357

?>      

答案 1 :(得分:0)

使用array_column选择列:

$selected_rowids = array_column($yourarrayname, 'row_id');

如果你想拥有一个id =&gt; row_id数组:

$selected_rowids = array_column($yourarrayname, 'row_id', 'id');

答案 2 :(得分:0)

$ value){         if(!in_array($ skip_values)){             echo $ value;         } else {             继续;         }     } ?&GT;