Home >Backend Development >PHP Tutorial >Functions in PHP8: Various practical applications of array_key_last() and array_key_first()

Functions in PHP8: Various practical applications of array_key_last() and array_key_first()

王林
王林Original
2023-05-16 08:43:351398browse

There are two new array functions in PHP8: array_key_last() and array_key_first(), which are used to return the last and first key name of the array. These two functions can help developers access arrays more conveniently and achieve more elegant and efficient programming. This article will introduce the use of these two functions and explain them based on actual application scenarios. I hope it will be helpful to PHP developers.

1. Basic usage of array_key_last() and array_key_first() functions

  1. array_key_last() function

array_key_last() function is used to return the array The last key name. For associative arrays, the last key refers to the last key in the order in which elements are inserted into the array. If the array is empty, the function returns NULL.

The following is a sample code using the array_key_last() function:

<?php
$my_array = array('apple', 'banana', 'orange');
$last_key = array_key_last($my_array);
echo "The last key of the array is: " . $last_key . "
";
?>

The execution results are as follows:

The last key of the array is: 2
  1. array_key_first() function
## The #array_key_first() function is used to return the first key name of the array. For associative arrays, the first key is the first key in the order in which the elements are inserted into the array. If the array is empty, the function returns NULL.

The following is a sample code using the array_key_first() function:

<?php
$my_array = array('apple', 'banana', 'orange');
$first_key = array_key_first($my_array);
echo "The first key of the array is: " . $first_key . "
";
?>

The execution results are as follows:

The first key of the array is: 0

2. Actual application scenarios

    Traversal Associative array
When using a for loop to traverse an associative array, we usually need to obtain the first and last key names in the array in order to determine the start and end conditions of the loop. Now, we can use array_key_first() and array_key_last() functions to achieve this.

The following is a sample code for traversing an associative array using the array_key_first() and array_key_last() functions:

<?php
$my_array = array('apple' => 1, 'banana' => 2, 'orange' => 3);
$first_key = array_key_first($my_array);
$last_key = array_key_last($my_array);
for ($i = $first_key; $i <= $last_key; $i++) {
  echo "The value of " . $my_array[$i] . " is " . $i . "
";
}
?>

The execution results are as follows:

The value of 1 is apple
The value of 2 is banana
The value of 3 is orange

    Get the last item in the array An element
Sometimes, we need to get the last element in the array, which can be achieved using the array_key_last() function.

The following is a sample code that uses the array_key_last() function to obtain the last element in the array:

<?php
$my_array = array('apple', 'banana', 'orange');
$last_index = array_key_last($my_array);
$last_element = $my_array[$last_index];
echo "The last element of the array is: " . $last_element . "
";
?>

The execution results are as follows:

The last element of the array is: orange

    Implementing a double-ended queue
A double-ended queue is a special queue that can perform insertion and deletion operations at both ends of the queue. When using a PHP array to implement a double-ended queue, you can use the array_key_last() and array_key_first() functions to easily obtain the head and tail of the queue.

The following is an example code for implementing a double-ended queue using the array_key_last() and array_key_first() functions:

<?php
class Deque {
  private $queue;
  
  public function __construct() {
    $this->queue = array();
  }
  
  public function addFirst($value) {
    array_unshift($this->queue, $value);
  }
  
  public function addLast($value) {
    $this->queue[] = $value;
  }
  
  public function removeFirst() {
    if (!empty($this->queue)) {
      $first_key = array_key_first($this->queue);
      unset($this->queue[$first_key]);
    }
  }
  
  public function removeLast() {
    if (!empty($this->queue)) {
      $last_key = array_key_last($this->queue);
      unset($this->queue[$last_key]);
    }
  }
  
  public function display() {
    foreach($this->queue as $value) {
      echo $value . " ";
    }
    echo "
";
  }
}

$deque = new Deque();
$deque->addFirst(1);
$deque->addFirst(2);
$deque->addLast(3);
$deque->addLast(4);
$deque->display(); // expected output: 2 1 3 4
$deque->removeFirst();
$deque->removeLast();
$deque->display(); // expected output: 1 3
?>

The execution results are as follows:

2 1 3 4
1 3

3. Summary

array_key_last() and array_key_first() functions are two new array functions in PHP8, used to return the last and first key name of the array. The use of these two functions can help us access arrays, traverse associative arrays, obtain the last element in the array, and implement double-ended queues more conveniently. Mastering the use of these two functions can enable us to write more elegant and efficient PHP code.

The above is the detailed content of Functions in PHP8: Various practical applications of array_key_last() and array_key_first(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn