PHP takes the first element of an array and deletes an array instance _php tutorial

Source: Internet
Author: User
In PHP array is a very special type of array can store various types of data, below I will describe the array read and delete some instances of the specified element.

Array: Arrays

Offset: Specifies the starting position of the element to be removed. If it is a positive number, start with the previous one, and if it is negative, take the offset from the back to the absolute value.

First, PHP takes an indexed array of one

It is very easy to take the first element of an indexed array, and subscript write 0, not much.


Focus on sharing PHP with the first method of associative array. First put the code that I wrote today:

The code is as follows Copy Code
Take the default first channel name
$channel _arr = $this->get_from_channel (); All channel arrays
$arr _num = count ($channel _arr);
$first _channel = Array_slice ($channel _arr,0,-($arr _num-1); Fetch Channel Array First
$html [' from_channel '] = $first _channel[0];


Delete an array

The code is as follows Copy Code


$a =array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");
Print_r (Array_slice ($a,));
?>

Output

Array ([0] = Cat [1] = Horse)

There is also the Array_shift () function to delete the first element in the array and return the value of the deleted element.

The relative Array_pop () function deletes the last element in the array.

Several functions used to feel array_search () more practical

The Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. Returns false if not found

The code is as follows Copy Code


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

$del _value = 3;
Unset ($array [Array_search ($del _value, $array)]);//use unset to delete this element

Print_r ($array);

Output

Array (' 1 ', ' 2 ', ' 4 ', ' 5 ');

Remove an element from an array in PHP

There are several ways to delete an array element in PHP:
To delete an element, use onset ()

The code is as follows Copy Code

Unset ($array [3]);
unset ($array [' foo ']);

To delete multiple discontinuous elements, also use unset ()

The code is as follows Copy Code

Unset ($array [3] $array [5]);
unset ($array [' foo '] $array [' Bar ']);

To delete multiple contiguous elements, use the Array_splice ()

The code is as follows Copy Code

Array_splice ($array $offset $length);

http://www.bkjia.com/PHPjc/631501.html www.bkjia.com true http://www.bkjia.com/PHPjc/631501.html techarticle in PHP array is a very special type of array can store various types of data, below I will describe the array read and delete some instances of the specified element. Array: Arrays off ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.