Home  >  Article  >  Backend Development  >  How to find and delete elements using PHP array manipulation

How to find and delete elements using PHP array manipulation

PHPz
PHPzOriginal
2023-04-26 10:31:09594browse

PHP is a very popular server-side scripting language that is widely used in web development. In PHP, arrays are a very common data type and are often used to store related data collections.

As the data continues to increase, sometimes it is necessary to find or delete certain elements in the array. In this article, we will discuss how to manipulate arrays using PHP to find and delete elements.

1. Basic operations of arrays

(1) Create an array

In PHP, you can create a new array through the array() function:

$numbers = array(1, 2, 3, 4, 5);

You can also use the [] operator to create an array:

$numbers = [1, 2, 3, 4, 5];

(2) Access array elements

When accessing an array element, you can use the [] operator plus the subscript of the element. Array subscripts start at 0, so the first element has a subscript of 0.

echo $numbers[0];    //输出1

(3) Modify the array element

When you need to modify the array element, you can use the array subscript to access the element and reassign it:

$numbers[0] = 10;

This will change the array element The first element is changed from 1 to 10.

2. Find array elements

(1) Linear search

In PHP, use a for loop to compare array elements one by one to find specific elements:

function linear_search($arr, $x) {
  for ($i=0; $i < count($arr); $i++) {
    if ($arr[$i] == $x) {
      return $i;
    }
  }
  return -1;
}

A function called linear_search is defined here, accepting two parameters: an array and the value to be found. It uses a for loop to iterate through the array, and once the target element is found, its index is returned.

The disadvantage of this search method is that in the worst case, the entire array needs to be compared, and the time complexity is O(n).

(2) Binary search

Binary search is a more efficient search method, which can shorten the search time to O(log n) in some cases.

This method only works on sorted arrays. Here's the basic idea: you can narrow your search by half each time.

function binary_search($arr, $x) {
  $low = 0;
  $high = count($arr) - 1;
    
  while ($low <= $high) {
    $mid = floor(($low + $high) / 2);
        
    if ($arr[$mid] < $x) {
      $low = $mid + 1;
    }
    else if ($arr[$mid] > $x) {
      $high = $mid - 1;
    }
    else {
      return $mid;
    }
  }
    
  return -1;
}

A function called binary_search is defined here, accepting two parameters: an array and the value to be found. It uses a while loop to locate where the target element is.

(3) Use in_array function to find

In PHP, you can use the built-in function in_array to find elements in the array:

$numbers = [10, 20, 30, 40, 50];

if (in_array(30, $numbers)) {
  echo "Found";
} else {
  echo "Not found";
}

This program will output "Found", Because the array contains 30.

3. Delete array elements

(1) Use the unset function to delete

In PHP, you can use the built-in function unset to delete elements in the array. If the element to be deleted is the last element of the array, you can use the array_pop function directly.

$numbers = [10, 20, 30, 40, 50];

unset($numbers[2]);    //删除30
array_pop($numbers);   //删除50

print_r($numbers);

This program will output [10, 20, 40] because 30 and 50 have been deleted.

(2) Use the array_splice function to delete

Another way to delete array elements is to use the array_splice function. This function can delete one or more elements simultaneously, starting from any position in the array.

$numbers = [10, 20, 30, 40, 50];

array_splice($numbers, 2, 1);  //删除30

print_r($numbers);

This program will also output [10, 20, 40, 50] because 30 is deleted.

Summary

PHP provides a lot of built-in functions and operators to handle arrays. Finding and deleting array elements is one of the operations frequently used in PHP development, so it is important to understand these techniques.

When looking for array elements, linear search and binary search are the most commonly used methods, depending on whether the array is sorted. When deleting array elements, you can use functions such as unset, array_pop, and array_splice.

No matter which method, you need to know the position and subscript of the target element to ensure that the array element is deleted correctly.

The above is the detailed content of How to find and delete elements using PHP array manipulation. 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