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);
4
  • Here is the $_SERVER array Is it? I see no array!! Commented Sep 13, 2018 at 17:36
  • Use array_keys function to get all the keys in array and then use array_shift. Similarly array_values for all the values in array. Commented Sep 13, 2018 at 17:38
  • 3
    Why would this ever be useful? Is that array ever guaranteed to be a particular order? Commented Sep 13, 2018 at 17:39
  • @Mati Urbaniak, did you try with array_slice()? Commented Sep 13, 2018 at 18:01

6 Answers 6

7

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.

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

2 Comments

Does a foreach loop cause array copies? This is an interesting solution, didn't know these functions were implemented.
foreach doesn't create array copies, but it does modify the array pointer. It doesn't matter in most situations, but still
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.

Comments

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.

Comments

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 */

1 Comment

Aside from the higher memory usage your solution is just as good as any other. It's a tiny amount of data in this case, but things might get iffy if the array contains a million items. That might explain the downvotes.
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));

Comments

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

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.