How can I use array_search with array_chunk?

45 Views Asked by At

I want to search and delete $srch_data data from the $list, but array_search() is not working. What's going wrong?

$srch_data = 'neha,[email protected]'; 

$list = "gaurav,[email protected],neha,[email protected],ayush,[email protected]";
$arr = explode(',',$list);
$list_array = array_chunk($arr,2);

$pos = array_search($srch_data,$list_array);
echo $pos;
1

There are 1 best solutions below

3
On BEST ANSWER

The problem is that you are searching with different things. Once you have used array_chunk(), your data is...

Array
(
    [0] => Array
        (
            [0] => gaurav
            [1] => [email protected]
        )

    [1] => Array
        (
            [0] => neha
            [1] => [email protected]
        )
...

and you are searching for

'neha,[email protected]'

so this will not match.

If you converted your search string to an array as well, this will work...

$pos = array_search(explode(",", $srch_data),$list_array);