Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array in PHP that looks like

Array ( [123654] => Array ( [0] => 123456789123456789 [1] => 1 [2] => 06/24/2011 [3] => 06/24/2012 [4] => 12355.44 [5] => 55321.55 ) ) 

I know in javascript I could access the data I need by doing array[0][0], how would I go about doing this in PHP. It is the 123456789123456789 value that I'm looking at getting.

share|improve this question

4 Answers 4

up vote 1 down vote accepted

If you don't know the exact keys, you could do something like this:

$a = array_values($my_array);
$b = array_values($a[0]);
echo $b[0];

array_values replaces the keys by simple numbers from 0 to n-1 (where n is the count of values), by that you can access your desired value with the indexes [0][0]. See more here

share|improve this answer
    
Perfect, just what I was looking for. Thanks. –  bmbaeb Jun 24 '11 at 16:27

Try this

array_slice($array, 0, 1);

http://php.net/array_slice

share|improve this answer
    
@MartinMatysiak This is not explicit enough. If his array keys change or elements within the array or sub-array are re-arranged, this will get the wrong values. –  FinalForm Jun 24 '11 at 16:24
    
Well, he wants the first value of the first sub-array, doesn't he? If the order changes, than another array or value is the respective first one, so that should be written. At least that is how I understand the question. –  Martin Matysiak Jun 24 '11 at 16:26

http://codepad.org/YXu6884R

Here you go. See above for proof. The methodology from @azat is not explicit enough and is prone to risk if the elements of the array or sub array are re-arranged or if the key value for the super array changes.

$my_array = array( 123654 => array( 0 => '123456789123456789', 1 => '1', 2 => '06/24/2011', 3 => '06/24/2012', 4 => '12355.44', 5 => '55321.55' ) );

echo $my_array['123654'][0];
share|improve this answer

Try

$first = array_shift(array_values($array));

http://php.net/manual/en/function.array-shift.php

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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