Home > Blockchain >  search value in multi array
search value in multi array

Time:12-08

I have two arrays (array_1 and array_2). This is the structure:

array_1

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

            [ID] => 489
        )

)

array_2

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

            [ID] => 13
        )

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

            [ID] => 48
        )

)

Now I would like to check, if the mail address "[email protected]" (array_1[0]['email'][0]) exist in array_2. And If yes: I need to know the key of array_2, where the mail address was found.

I tried array_search() but this seems not work with multi arrays. Can you help me please? Thanks !!

array_1 var_export()

array (
  0 => 
  array (
    'email' => 
    array (
      0 => '[email protected]',
    ),
    'customerID' => '489',
  ),
)

array_2 var_export()

array (
  0 => 
  array (
    'email' => 
    array (
      0 => '[email protected]',
    ),
    'customerID' => '13',
  ),
  1 => 
  array (
    'email' => 
    array (
      0 => '[email protected]',
      1 => '[email protected]',
    ),
    'customerID' => '48',
  ),
)

CodePudding user response:

You could use array_filter combined with array_search. If an array is found, it automatically casts to a boolean (true/false) thus returning the parent array.

Update: array_search returns the index of value: if the index is 0, it is casted as false so a strict-type check for false should be made which fixes any bugs.

See it working over at 3v4l.org

array_filter($array, fn($x) => array_search('[email protected]', $x['email']) !== false);

Output:

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

            [customerID] => 48
        )

)

Update: If array_search is throwing unexpected results, try using in_array instead perhaps.

array_filter($array, fn($x) => in_array('[email protected]', $x['email']));

Update: Any empty results, without context, I assume it isn't an array and in-fact is a string so you could append the checks to the function:

&& is_array($x['email']) && !empty($x['email'])

CodePudding user response:

You could try using array_walk_recursive
A crude example:

$email = '[email protected]'

function checkEmail($item, $key)
{
    if ($item === $email) {
       echo $key;
    }
}

array_walk_recursive($array2, 'checkEmail');

More in docs https://www.php.net/manual/en/function.array-walk-recursive.php

CodePudding user response:

Since you need the subarray index of where it was found, you could

  • Loop on each subarray and call a search function, say checkValueExistence which returns true or false depending on the search result. If the func returns true, return the index, else -1 or false indicating the value(in your case email) was not found.

  • checkValueExistence will simply loop through the array at hand and check if any value matches the supplied one and if the current iteration value in context is an array in itself, it recursively calls itself for this new subarray.

Snippet:

<?php

function getValueIndex($data, $val){
    foreach($data as $idx => $d){
        if(checkValueExistence($d, $val) === true){
            return $idx;
        }
    }
    
    return -1;// or false meaning not found
}

function checkValueExistence($data, $val){
    foreach($data as $value){
        if(is_array($value) && checkValueExistence($value, $val) || !is_array($value) && $value === $val) return true;
    }
    return false;
}

Online Demo

  • Related