Search code examples
phpmultidimensional-arraycompare

PHP array_search is very slow - how can i speed up?


I have two arrays one is a response from an API call where I get information about which email-addresses are already uploaded in the System -> $array_emails_found

The other array is data which was uploaded by a form -> $array_data

In this use case we need to find out which user is in the system and we need to flag the user data.

Here is my code:

for ($i = 0; $i < $count_emails_found; $i++){
    if ($key = array_search($array_emails_found[$i], array_column($array_data, 'Email'))){
        $key = $key + 1;
        $array_data[$key]["InEloqua"] = $array_data[$key]["InEloqua"] . "Y";
    }
} 

The problem is, that the uploaded file can include for example over 450k users and the response can also deliver such a number and it takes over 2h to be finished.


Solution

  • Thank you @Barmar for your comment, now it works much faster

    $arraycolumn = array_column($array_data, 'Email');
    
    for ($i = 0; $i < $count_emails_found; $i++){
            $key = array_search($array_emails_found[$i], $arraycolumn);
            $key = $key + 1;
            $array_data[$key]["InEloqua"] = $array_data[$key]["InEloqua"] . "Y";
    }