1

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.

4 Answers 4

5

Try this

array_slice($array, 0, 1);

http://php.net/array_slice

Sign up to request clarification or add additional context in comments.

2 Comments

@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.
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.
2

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

Comments

0

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];

Comments

0

Try

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.