Hallo!

I have some array:

$arr1 = array ("a", "b");

$arr2 = array ("a", "b", "c", "d", "e");

Haw can I remove element "a" and "b" from arr2?

I want second array look like this: $arr2 = array ("c", "d", "e");

😃

Thanxs!

    $arr2 = array_slice($arr2, 2);

    Will work, there are other functions that can get you the same result too. Also take a look at array_shift() and array_splice().

      Thank you for quick replay!

      But arr1 cames from database and have random value.
      Such as arr1 = array ("a", "c", "e") or arr1 = array ("c", "e", "d") etc ....

      arr2 is static array.

      I want remove this elements from arr2 ...😕

        Then there's no easy way of doing it through PHP. You might be able to do it by looping thru the arrays, but it's very messy. An easier way to do this would be load the content of $arr2 into a table of your database, and use a query to eliminate the entries in $arr1:

        SELECT * FROM arr2 WHERE col_x NOT IN (SELECT col_x FROM arr1);
        
          $arr2 = array_merge(array_diff($arr2, $arr1));

            This work perfect.
            Thanks man!
            😃

              Haha, seems like I've been away PHP way too long. But for some reason, I thought there were some problems with array_diff() and array_merge() functions that can give weird results before. Maybe that has been fixed!

                Write a Reply...