0

I have this cart object array

 Array
(
    [16] => Array
        (
            [count] => 1
            [data] => CartItem Object
                (
                    [_itemID] => 16
                    [_itemData] => 
                )
        )

    [14] => Array
        (
            [count] => 1
            [data] => CartItem Object
                (
                    [_itemID] => 14
                    [_itemData] => 
                )
        )

    [18] => Array
        (
            [count] => 1
            [data] => CartItem Object
                (
                    [_itemID] => 18
                    [_itemData] => 
                )
        )

    [15] => Array
        (
            [count] => 1
            [data] => CartItem Object
                (
                    [_itemID] => 15
                    [_itemData] => 
                )
        )
)

From this array I want to get these key values 16, 14, 18, 15.

How can I get this ?

4

5 回答 5

4

您可以使用该array_keys功能。

http://php.net/array_keys

于 2013-08-08T14:15:15.180 回答
4

array_keys将给出特定数组的键:

$keys = array_keys($yourArray);
print_r($keys);
于 2013-08-08T14:15:19.283 回答
2

你也可以在 foreach 循环中做到这一点

foreach($array as $key=>$nextArray){
    //Process
}
于 2013-08-08T14:22:27.280 回答
1

要返回数组的所有键,请使用(http://php.net/manual/en/function.array-keys.php):

array_keys($array);
于 2013-08-08T14:16:27.233 回答
0

查看应该做你想做的事情的 php foreacharray_push文档。

$numbers = new array();
foreach ($cart as $key => $value) {
    array_push($numbers, $key);
}
于 2013-08-08T14:20:55.373 回答