0

I have an array (as shown below). It has a and b, the numbers inside of each of them. However, when I use the end() function, it gives me b's array. I want the actual b letter. to be printed, not the number array. How can I do this?

<?php

$array = array("a" => array(1, 2, 3),
                       "b" => array(4, 5, 6));

$end = end($array);
print_r($end); // gives me 4, 5, 6. I want the value b
Yuri
  • 89
  • 1
  • 1
  • 9
  • 1
    Possible duplicate of [How to get last key in an array?](https://stackoverflow.com/questions/2348205/how-to-get-last-key-in-an-array) – Dom Jan 23 '19 at 23:16
  • @Yuri see the duplicate - there are many more solutions there. – Nick Jan 23 '19 at 23:21
  • `end($array); then `$end = key($array)` the call to end puts the internal array pointer at the end of the array, then key gets the key of the current position. – ArtisticPhoenix Jan 23 '19 at 23:22

3 Answers3

1

Use end with array_keys instead:

$array = array("a" => array(1, 2, 3),
                       "b" => array(4, 5, 6));
$keys = array_keys($array);
$end = end($keys);
print_r($end);

Note that since end adjusts the array pointer (hence you can't pass the output from array_keys directly to end without a notice level error), it's probably preferable to simply use

echo $keys[count($keys)-1];
Nick
  • 138,499
  • 22
  • 57
  • 95
  • is there a more efficient way of find the last item (i am always looking for b) – Yuri Jan 23 '19 at 23:18
  • @Cemal yes, it still works in that case. array_keys only gives the keys of the array you pass it. to get the keys of an element of that array which is also an array, you'd have to do something like `array_keys($array['b])`. See https://3v4l.org/EVNLu – Nick Jan 23 '19 at 23:58
1

Simply

$array = array("a" => array(1, 2, 3),"b" => array(4, 5, 6));
                
end($array);

echo key($array);

Output

b

Sandbox

the call to end puts the internal array pointer at the end of the array, then key gets the key of the current position (which is now the last item in the array).

To reset the array pointer just use:

 reset($array); //moves pointer to the start

You cant do it in one line, because end returns the array element at the end and key needs the array as it's argument. It's a bit "Weird" because end moves the internal array pointer, which you don't really see.

Update

One way I just thought of that is one line, is to use array reverse and key:

 echo key(array_reverse($array));

Basically when you do array_reverse it flips the order around and returns the reversed array. We can then use this array as the argument for key(), which gets the (current) first key of our now backwards array, or the last key of the original array(sort of a double negative).

Output

 b

Sandbox

Enjoy!

Community
  • 1
  • 1
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
0

A simple way to do it would be to loop through the keys of the array and store the key at each index.

<?php

$array = array("a" => array(1, 2, 3),
"b" => array(4, 5, 6));

foreach ($array as $key => $value) $end = $key;
// This will overwrite the value until you get to the last index then print it out

print_r($end);
Cemal
  • 1,469
  • 1
  • 12
  • 19
G Schunk
  • 23
  • 7