2

From a given array (eg: $_SERVER), I need to get the first and last key and value. I was trying use array_shift to get first value and key but what I get is value.

Here is the $_SERVER array:

print_r($_SERVER, true))

And I tried with:

echo array_shift($_SERVER);
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
Mati Urbaniak
  • 45
  • 3
  • 8

6 Answers6

6

With PHP >= 7.3 you can get it fast, without modification of the array and without creating array copies:

$first_key = array_key_first($_SERVER);
$first_value = $_SERVER[$first_key];

$last_key = array_key_last($_SERVER);
$last_value = $_SERVER[$last_key];

See array_key_first and array_key_last.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • Does a foreach loop cause array copies? This is an interesting solution, didn't know these functions were implemented. – Blue Sep 13 '18 at 17:45
  • `foreach` doesn't create array copies, but it does modify the array pointer. It doesn't matter in _most_ situations, but still – Alex Shesterov Sep 13 '18 at 17:52
2

It's not clear if you want the value, or the key. This is about as efficient as it gets, if memory usage is important.

If you want the key, use array_keys. If you want the value, just refer to it with the key you got from array_keys.

$count = count($_SERVER);
if ($count > 0) {
  $keys = array_keys($_SERVER);
  $firstKey = $keys[0];
  $lastKey = $keys[$count - 1];
  $firstValue = $array[$firstKey];
  $lastValue = $array[$lastKey];
}

You can't use $count - 1 or 0 to get the first or last value in keyed arrays.

Christian
  • 927
  • 2
  • 13
  • 22
1

You can do a foreach loop, and break out after the first one:

foreach ( $_SERVER as $key => $value ) {
    //Do stuff with $key and $value
    break;
}

Plenty of other methods here. You can pick and choose your favorite flavor there.

Blue
  • 22,608
  • 7
  • 62
  • 92
0

What about this:

$server = $_SERVER;
echo array_shift(array_values($server));
echo array_shift(array_keys($server));

reversed:

$reversed = array_reverse($server);
echo array_shift(array_values($reversed));
echo array_shift(array_keys($reversed));
Gaspar Teixeira
  • 1,237
  • 11
  • 21
0

Separate out keys and values in separate arrays, and extract first and last from them:

// Get all the keys in the array
$all_keys = array_keys($_SERVER);

// Get all the values in the array
$all_values = array_values($_SERVER);

// first key and value
$first_key = array_shift($all_keys);
$first_value = array_shift($all_values);

// last key and value (we dont care about the pointer for the temp created arrays)
$last_key = end($all_keys);
$last_value = end($all_values);

/* you can use reset function after end function call 
   if you worry about the pointer */
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
0

I think array_slice() will do the trick for you.

<?php
$a = array_slice($_SERVER, 0, 1);
$b = array_slice($_SERVER, -1, 1, true);
//print_r($_SERVER);
print_r($a);
print_r($b);

?>

OUTPUT

Array ( [TERM] => xterm )
Array ( [argc] => 1 )

DEMO: https://3v4l.org/GhoFm

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103