PHP removes two methods for specific elements in an array _php tutorial

Source: Internet
Author: User
Method One:
Copy CodeThe code is as follows:
$arr 1 = Array (1, 3, 5,7,8);
$key = Array_search (3, $arr 1);

if ($key!== false)
Array_splice ($arr 1, $key, 1);
Var_dump ($arr 1);
?>

Output:
Array (4) {[0]=> int (1) [1]=> int (5) [2]=> int (7) [3]=> int (8)}

Method Two:
Copy the Code code as follows:
$arr 2 = Array (1, 3, 5,7,8);
foreach ($arr 2 as $key = $value)
{
if ($value = = = 3)
unset ($arr 2[$key]);
}
Var_dump ($arr 2);
?>

Output:
Array (4) {[0]=> int (1) [2]=> int (5) [3]=> int (7) [4]=> int (8)}


Summary: You can see the difference between using Array_splice () to remove a specific value and using unset to remove a specific value.

If the Array_splice () function is deleted, the index value of the array also changes.

If the unset () function is removed, the index value of the array does not change.

http://www.bkjia.com/PHPjc/328026.html www.bkjia.com true http://www.bkjia.com/PHPjc/328026.html techarticle method One: Copy the code as follows: PHP $arr 1 = array (1,3, 5,7,8), $key = Array_search (3, $arr 1), if ($key!== false) Array_splice ($arr 1, $key, 1); Var_dump ($arr 1);? Output: ...

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