Home  >  Article  >  Backend Development  >  Example of deleting elements from php array

Example of deleting elements from php array

WBOY
WBOYOriginal
2016-07-25 08:53:40886browse
  1. $arr = array('a','b','c','d');
  2. unset($arr[1]);
  3. print_r($arr);
  4. ?>
Copy the code

After using unset before, the array $arr should compress the array to fill the missing element positions, but after print_r($arr), the final result is Array ( [0] => a [2] => c [3] => d );

Let’s take a look at the form of a digital array:

  1. $arr = range(5,10,4);
  2. print_r($arr);//Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 [4] => 9 [5] => 10 )< /span>
  3. unset($arr[1]);//Array ( [0] => 5 [2] => ; 7 [3] => 8 [4] => 9 [5] => 10 )
  4. print_r($arr);
  5. ?>
Copy the code to see The output form is also an array that will fill in the positions of missing elements. So how can we ensure that missing elements are filled in and the array is re-indexed? Use: array_splice():
Example:

$arr = array('a','b','c','d');
  • array_splice($arr,1,1);
  • print_r($arr) ; // Array ( [0] => a [1] => c [2] => d )< /span>
  • ?>
  • Copy code
  • 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