Home  >  Article  >  Backend Development  >  How to remove specified subscript from array in php

How to remove specified subscript from array in php

PHPz
PHPzOriginal
2023-04-19 09:18:45571browse

During development using PHP, we often need to operate on arrays. Sometimes, we need to remove specified subscripts in the array to achieve better data processing results. This article will share how to remove specified subscripts from arrays in PHP. The content covers the following aspects:

  1. Review of basic knowledge of PHP arrays
  2. How to remove specified subscripts from arrays in PHP
  3. How PHP traverses the array and removes the specified subscript
  4. PHP example demonstration
  5. Summary

1. Review of basic knowledge of PHP arrays

Before proceeding with array processing, we need to understand some basic knowledge of PHP arrays.

An array in PHP can be a variable containing zero or more elements. Each element of the array has a unique subscript that identifies the element. Arrays in PHP can use either numeric or string subscripts. Arrays using numeric subscripts are called indexed arrays, and arrays using string subscripts are called associative arrays.

We can use the following two methods to create PHP arrays:

  1. Use array initialization syntax

Use array initialization syntax, directly when assigning values ​​to variables Define the contents of the array. For example:

$fruit = array('apple', 'banana', 'cranberry');

  1. Use subscript assignment syntax

Use Subscript assignment syntax: After assigning a value to a variable, assign values ​​to the array elements one by one. For example:

$fruit[0] = 'apple';
$fruit[1] = 'banana';
$fruit[2] = 'cranberry';

After creating the array, we can use some built-in functions to operate on the array:

array_filter(): Filter the elements in the array

array_map(): Operate on each element in the array

array_search(): Find the position of an element in the array

array_slice(): Get a new array from the array

array_splice(): Delete and insert from the array Element

in_array(): Check whether the specified value exists in the array

count(): Get the number of elements in the array, etc.

2. How to remove specified subscripts from arrays in PHP

In PHP, there are many ways to remove specified subscripts from arrays, some of which are as follows:

  1. Use the unset() function

We can use the unset() function to remove the element with a specified subscript in the array. The unset() function allows you to reference and remove elements from a variable by name. For example:

$fruit = array('apple', 'banana', 'cranberry');

unset($fruit[0]); // Remove the first item in the array elements

print_r($fruit); // Output result: Array ([1] => banana [2] => cranberry)

  1. Use the array_splice() function

We can use the array_splice() function to remove elements and reorder the array. This function is used to take out a portion of an array and replace the specified elements with other values. For example:

$fruit = array('apple', 'banana', 'cranberry');

array_splice($fruit, 1, 1);

print_r( $fruit); // Output result: Array ( [0] => apple [1] => cranberry )

In the above example, the array_splice() function uses the first parameter to specify the operation to be performed An array, the second parameter specifies the starting index of the element to be deleted, and the third parameter specifies the number of elements to be deleted.

  1. Use the array_diff() function

We can use the array_diff() function to compress the array, that is, compare the specified array with other arrays and return different elements. For example:

$fruit = array('apple', 'banana', 'cranberry');

$fruit = array_diff($fruit, array('apple'));

print_r($fruit); //Output result: Array ([1] => banana [2] => cranberry)

In the above example, the array_diff() function uses two parameters, the first is the array to be processed, and the second is the element to be deleted from the array.

3. How does PHP traverse the array and remove the specified subscript

In actual development, we usually need to traverse the array and remove the specified subscript. The following are some methods of traversing and deleting array elements:

  1. Using foreach loop

We can use foreach loop to traverse the array and specify the following through the unset() function The target element is deleted. For example:

$fruit = array('apple', 'banana', 'cranberry');

foreach($fruit as $key => $value){

if($value == 'apple'){

unset($fruit[$key]);

}

}

print_r ($fruit); // Output result: Array ( [1] => banana [2] => cranberry )

In the above example, the name of the key (i.e. subscript) will be stored in In the $key variable, the value will be stored in the $value variable. If the value is equal to "apple", use the unset() function to delete the element.

  1. Using for loop

We can use for loop to traverse the array and delete the element with the specified subscript through the unset() function. For example:

$fruit = array('apple', 'banana', 'cranberry');

for($i=0;$i

if($fruit[$i] == 'apple'){

unset($fruit[$i]);

}

}

print_r($fruit); //Output result: Array ([1] => banana [2] => cranberry)

In the above example, we use a for loop to traverse the array, And use the unset() function to delete array elements under specific conditions.

4. PHP Example Demonstration

The following is an example program that uses PHP to remove specified subscripts from an array:

$fruit = array ('apple', 'banana', 'cranberry');

//Use the unset() function to delete the element with the specified subscript

unset($fruit[0]);

print_r($fruit);

//Use the array_splice() function to delete elements and reorder them

$fruit = array('apple', 'banana', 'cranberry') ;

array_splice($fruit, 1, 1);

print_r($fruit);

//Use the array_diff() function to compress the array

$fruit = array('apple', 'banana', 'cranberry');

$fruit = array_diff($fruit, array('apple'));

print_r($fruit );

//Use a foreach loop to traverse the array and delete the element with the specified subscript

$fruit = array('apple', 'banana', 'cranberry');

foreach($fruit as $key => $value){

if($value == 'apple'){

unset($fruit[$key]);

}

}

print_r($fruit);

//Use a for loop to traverse the array and delete the element with the specified subscript

$ fruit = array('apple', 'banana', 'cranberry');

for($i=0;$i

if ($fruit[$i] == 'apple'){

unset($fruit[$i]);

}

}

print_r($fruit);

?>

5. Summary

This article details how to remove elements with specified subscripts in an array in PHP. We can achieve this effect using the functions and standard traversal methods already listed.

In actual development, it may be useful to delete the specified subscript of the array, and the various built-in methods provided by PHP can help us complete this operation. Therefore, we can choose the most appropriate method in specific application scenarios and personal preferences.

The above is the detailed content of How to remove specified subscript from array in php. 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