Search code examples
phparraysreindex

Reindex array after filtering with array_intersect()


I want to find the intersection of elements between two arrays; the result will go to another array.

I've written: $result = array_intersect($arrayone,$arraytwo);

If I write count($result) it returns a correct value, but if I write $result[0] it returns the following notice: Notice: Undefined offset: 0.


Solution

  • the intersection maintains index. do the following

    $result = array_intersect($arrayone,$arraytwo);
    $result = array_values($result);
    

    Then you can access with $result[0];