0

There is probably a really simple way to do this but i can't work it out.

Array ( [0] => Array ( [] => US [1] => U.S. [2] => 21 [3] => 34 [4] => 33 [5] => 35 [6] => 39 [7] => 50 [8] => 61 ) [1] => Array ( [] => 79 [1] => 45 [2] => 84 [3] => 89 [4] => 59 [5] => 64 [6] => 34 [7] => 58 [8] => 55 ) [2] => Array ( [] => 63 [1] => 105 [2] => 68 [3] => 62 [4] => 64 [5] => 67 [6] => CL [7] => Chile [8] => 56 ) [3] => Array ( [] => 40 [1] => 40 [2] => 63 [3] => 37 [4] => 57 [5] => 64 [6] => 59 [7] => 53 [8] => 68 ) [4] => Array ( [] => 70 [1] => 66 [2] => 88 [3] => 48 [4] => 76 [5] => 83 [6] => 80 [7] => 53 [8] => 45 ) [5] => Array ( [] => 44 [1] => 51 [2] => 52 [3] => [4] => [5] => [6] => [7] => [8] => ) ) 

This is my array. There will all ways be the same number of object (9) in each however the number currently at 6 may increase.

I need to get the 1st item in each so for the 1st one i need (US) I'm stuck as if i put

echo $array[0][1];

Then I get U.S. however i need the first item (US) so i tried both

echo $array[0][0];
echo $array[0][];

Neither return a value. What am i doing wrong?

asked Aug 25, 2017 at 14:16
11
  • 4
    In your example, there is no key for the first array element, which I'm pretty sure is not possible Commented Aug 25, 2017 at 14:18
  • 2
    @GrumpyCrouton yes, an empty array key is possible in PHP; to access such an element then one must explicitly specify an "empty key" though - $foo[''] Commented Aug 25, 2017 at 14:19
  • So should be something like echo $array[0][''];. It's bad data structure design in my opinion... Commented Aug 25, 2017 at 14:20
  • @CBroe Man I'm learning a lot today! Commented Aug 25, 2017 at 14:21
  • 1
    @WilliamPerron - while that's a great function, it doesn't always give the first element, it gives the element at the current pointer, which may not be the first. (Hence the name current) Commented Aug 25, 2017 at 14:22

3 Answers 3

2

Using an "empty" string as key in an associative array is possible in PHP.

It is distinctively different from any other, non-empty string key values - so it fulfills the most basic requirement you have for such a key (and the PHP guys didn’t seem to see any need to explicitly prevent this.)

$arr = [
 'foo' => 'bar',
 '' => 'baz',
];

print_r would show this as

Array
(
 [foo] => bar
 [] => baz
)

and var_dump’ed it would look like this,

array(2) {
 ["foo"]=>
 string(3) "bar"
 [""]=>
 string(3) "baz"
}

The latter makes it more obvious that the key is in fact an empty string, and therefor $arr[""] resp. $arr[''] can be used to access this element.

One might consider this one of PHP’s peculiarities - it does work, but it is hardly ever used in practice, because it just does not make the most sense for most use cases.

answered Aug 25, 2017 at 14:32
0

The other answers offer good explanation about the empty key that I won't reiterate, but a simple way to get the first item in each of the sub-arrays is to map the reset function which we mentioned in the comments over your main array.

$firsts = array_map('reset', $your_array);

This should work regardless of what the key is.

answered Aug 25, 2017 at 14:38
0

If I were you, I would begin by asking myself why my array has an empty key, my best guess is that you set your subarrays with keys instead of letting it being indexed incrementally by writing something like this :

array(
 '' => 'US', // Maybe a '0' was intended to be there, and it's a type.
 '1' => 'U.S.',
 '2' => '21',
 '3' => '34',
 '4' => '33',
 // etc...
);

In that case, you may benefit from fixing your code, or at least updating it so that your array is confortable to use, for example by removing the keys so that they are replaced with successive indexes.

Anyway, if you want to use that current array, do this :

echo $array[0][''];

Or iterate through it :

foreach ($array as $sub) {
 echo $sub[''],'<br>';
}

If you don't have control over how the array is set, you can also reindex it using array_values(). That function takes an array as an argument and returns its values with successive indexes instead of its original keys :

foreach ($array as $key => $sub) {
 $array[$key] = array_values($sub);
}

That code should give you the same array than before with the exception that the empty keys are replaced with 0.

answered Aug 25, 2017 at 14:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.