PHP deletes the specified element in an array based on key implementation method

Source: Internet
Author: User
The following small series for everyone to bring an article according to the key to delete the specified element in the implementation method. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

The elements in the PHP array exist in a key-value pair (' key ' = ' value '), and sometimes we need to delete an element specified in the array based on the key.

  function Bykey_reitem ($arr, $key) {     if (!array_key_exists ($key, $arr)) {       return $arr;     }     $keys = Array_keys ($arr);     $index = Array_search ($key, $keys);     if ($index!== FALSE) {       array_splice ($arr, $index, 1);     }     return $arr;   }   $data = Array (' name ' = ' apple ', ' age ' =>12, ' address ' = ' Chinaguangzhou ');   $result = Array_remove ($data, ' name ');   Var_dump ($result);

Instructions for using the function:

1, Array_search ()

Definition and usage

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 it is not found.

Before PHP 4.2.0, the function returned null instead of false on failure.

If the third parameter, strict, is specified as true, the key name of the corresponding element is returned only if the data type and value are consistent.

Grammar

Array_search (value,array,strict)
Parameter description
Value is required. Specifies the value to search for in the array.
Array required. The array to be searched.
Strict is optional. Possible values:
True
False-Default

If the value is set to True, the type of the given value is also checked in the array

Example 1

  <?php    $a =array ("a" = "Dog", "b" = "Cat", "C" and "Horse");    Echo Array_search ("Dog", $a);  ? >

2, Array_splice ()

Definition and usage

The Array_splice () function, like the Array_slice () function, selects a series of elements in an array, but does not return, but instead deletes them and replaces them with other values.

If the fourth argument is supplied, the previously selected elements will be replaced by the array specified by the fourth parameter. The last generated array will be returned.

Grammar

Array_splice (Array,offset,length,array)

Parameter description

array required. Specifies the array.

offset required. Numerical. If offset is positive, the offset specified by the value in the input array begins to be removed. If offset is negative, it is removed from the offset specified by the value of the countdown at the end of the input array.

length is optional. Numerical. If this argument is omitted, all parts of the array from offset to end are moved. If length is specified and positive, then so many elements are removed. If length is specified with a negative value, all elements in the middle of offset to the end of the array ending with length are removed.

the element to which the array is removed is substituted by the elements in this array. If no value is removed, the elements in this array are inserted into the specified position.

Hints and Notes

Tip: If the function does not delete any elements (length=0), the alternate array is inserted from the position of the start parameter. (see example 3)

Note: The keys in the alternate array are not preserved.

Instance

<?php     $a 1=array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");     $a 2=array (0=> "Tiger",1=> "Lion");     Array_splice ($a 1,0,2, $a 2);     Print_r ($a 1);     Output:    array ([0] = Tiger [1] = Lion [2] = = Horse [3] = Bird)?>    //Same as Example 1, but output returns an array of: <?php
   $a 1=array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");     $a 2=array (0=> "Tiger",1=> "Lion");     Print_r (Array_splice ($a 1,0,2, $a 2));? >    //output:    Array ([0] = Dog [1] = Cat    )//length parameter set to 0:<?php     $a 1=array (0=> "Dog", 1=& gt; " Cat ");     $a 2=array (0=> "Tiger",1=> "Lion");     Array_splice ($a 1,1,0, $a 2);     Print_r ($a 1);? >   //output:   Array ([0] = Dog [1] = Tiger [2] = Lion [3] = Cat)

The above is PHP according to the key to delete the elements specified in the array implementation method of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.