How to remove values from an array in PHP?

前端 未结 6 410
终归单人心
终归单人心 2020-12-06 10:03

Is there a PHP function to remove certain array elements from an array?

E.g., I have an array (A) with values and another array (B) from wh

相关标签:
6条回答
  • 2020-12-06 10:22

    Use array_diff()

    $new_array = array_diff($arrayB, $arrayA);
    

    will return an array with all the elements from $arrayB that are not in $arrayA.

    To do this with associative arrays use array_diff_assoc().

    To remove a single value use:

    unset($array[$key]);
    

    You can of course loop that to do the equivalent of the array functions but there's no point in that.

    0 讨论(0)
  • 2020-12-06 10:29

    The array_diff function will do this for you.

    0 讨论(0)
  • 2020-12-06 10:31
    $array1 = array("a" => "green", "red", "blue", "red");
    $array2 = array("b" => "green", "yellow", "red");
    $result = array_diff($array1, $array2);
    
    print_r($result);
    
    0 讨论(0)
  • 2020-12-06 10:38

    I came accros this post looking for a way to remove one single value from an array (which the title states). Here is a straight forward way, assuming the value to remove is in the array:

    $list = array('foo', 'bar', 'yay', '\o/');
    $toremove = 'foo';
    unset($list[array_search($toremove, $list)]);
    

    Though this will throw errors if the element to remove is not part of the array.

    Another solution, but not really optimised performance wise is:

    $list = array('foo', 'bar', 'yay', '\o/');
    $toremove = 'foo';
    $list = array_flip($list);
    unset($list[$toremove]);
    $list = array_flip($list);
    

    Anyway, perhaps creating an array with the single value as using array_diff as suggested by everyone here is quicker and more efficient.

    0 讨论(0)
  • 2020-12-06 10:39

    Perhaps a simple and quicker solution is this. A simple reiteration that can delete(unset) multiple values in the array.

        // Your array
        $list = array("apple", "orange", "strawberry", "lemon", "banana");
    
        // Initilize what to delete
        $delete_val = array("orange", "lemon", "banana");
    
        // Search for the array key and unset   
        foreach($delete_val as $key){
            $keyToDelete = array_search($key, $list);
            unset($list[$keyToDelete]);
        }
    
    0 讨论(0)
  • 2020-12-06 10:40

    It depends on what you mean by "remove".

    You can use the unset() function to remove keys from your array, but this will not reindex it. So for example, if you have:

    $a = array(1 => 'one', 2 => 'two', 3 => 'three');
    

    and you then call

    unset($a[2]);
    

    You'll end up with something like

    (1 => 'one', 3 => 'three');
    

    If you need the array to be sequentially indexed, you can take the unsetted array and feed it into array_values(), which will return a new array with sequentially indexed keys.

    Returning to your original scenario, as others observe, array_diff will do the job for you, but note that it doesn't do an index check. If you need that, use array_diff_assoc, instead.

    0 讨论(0)
提交回复
热议问题