Aydın Aleydin
Aydın Aleydin

Reputation: 17

how to get value from array with 2 keys

i have array like

Array
 (
[1] => Array
    (
        [user_info] => Array
            (
                [id] => 1
                [name] => Josh
                [email] => [email protected]
                [watched_auctions] => 150022 150031
            )

        [auctions] => Array
            (
                [150022] => Array
                    (
                        [id] => 150022
                        [title] => Title of auction
                        [end_date] => 2013-08-28 17:50:00
                        [price] => 10
                    )

                [150031] => Array
                    (
                        [id] => 150031
                        [title] => Title of auction №
                        [end_date] => 2013-08-28 16:08:03
                        [price] => 10
                    )

            )

    )

so i need put in <td> info from [auctions] => Array where is id,title,end_date but when i do like $Info['id'] going and put id from [user_info] when i try $Info[auctions]['id'] there is return null how to go and get [auctions] info ?

Upvotes: 0

Views: 126

Answers (4)

inquam
inquam

Reputation: 12932

Your question is a bit malformed. I don't know if this is due to a lacking understanding of the array structure or just that you had a hard time to explain. But basically an array in PHP never has two keys. I will try to shed some more light on the topic on a basic level and hope it helps you.

Anyway, what you have is an array of arrays. And there is no difference in how you access the contents of you array containing the arrays than accessing values in an array containing integers. The only difference is that what you get if you retrieve a value from your array, is another array. That array can you then in turn access values from just like a normal array to.

You can do all of this in "one" line if you'd like. For example

echo $array[1]["user_info"]["name"]

which would print Josh

But what actually happens is no magic. You retrieve the element at index 1 from your array. This happens to be an array so you retrieve the element at index *user_info* from that. What you get back is also an array so you retrieve the element at index name.

So this is the same as doing

$arrayElement = $array[1];
$userInfo = $arrayElement["user_info"];
$name = $userInfo["name"];

Although this is "easier" to read and debug, the amount of code it produces sometimes makes people write the more compact version.

Since you get an array back you can also do things like iterating you array with a foreach loop and within that loop iterate each array you get from each index within the first array. This can be a quick way to iterate over multidimensional array and printing or doing some action on each element in the entire structure.

Upvotes: 0

Prashant Parekh
Prashant Parekh

Reputation: 428

$array =array();
foreach($mainArray as $innerArray){

$array[] = $innerArray['auctions'];
}

foreach($array as $key=>$val){
foreach($val as $k=>$dataVal){
    # Here you will get Value of particular key
    echo $dataVal[$k]['id'];

}
}

Try this code

Upvotes: 0

Nil&#39;z
Nil&#39;z

Reputation: 7475

Try:

foreach( $info['auctions'] as  $key=>$each ){
    echo ( $each['id'] );
}

Or,

foreach( $info as $key=>$each ){
    foreach( $each['auctions'] as  $subKey=>$subEach ){
        echo ( $subEach['id'] );
    }    
}

Upvotes: 1

Koraktor
Koraktor

Reputation: 42893

Given the data structure from your question, the correct way would be for example:

$Info[1]['auctions'][150031]['id']

Upvotes: 1

Related Questions