How to remove values from an array in PHP?

前端 未结 6 436
终归单人心
终归单人心 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.

提交回复
热议问题