Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ?

share|improve this question

closed as off-topic by Yogesh Suthar, Tomasz Kowalczyk, sasha.sochka, Stephen, pnuts Aug 8 at 23:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – Yogesh Suthar, Tomasz Kowalczyk, sasha.sochka, Stephen, pnuts
If this question can be reworded to fit the rules in the help center, please edit the question.

5 Answers

up vote 4 down vote accepted

array_keys will give the keys of a particular array:

$keys = array_keys($yourArray);
print_r($keys);
share|improve this answer

You can use the array_keys function.

http://php.net/array_keys

share|improve this answer

Also you could do that in a foreach loop

foreach($array as $key=>$nextArray){
    //Process
}
share|improve this answer

To return all the keys of the array use (http://php.net/manual/en/function.array-keys.php):

array_keys($array);
share|improve this answer

Check out the php foreach and array_push documentation that should do what you want.

$numbers = new array();
foreach ($cart as $key => $value) {
    array_push($numbers, $key);
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.