在多数组中搜索值

search value in multi array

我有两个数组(array_1 和 array_2)。 这是结构:

array_1

Array
(
    [0] => Array
        (
            [email] => Array
                (
                    [0] => mail@domain.de
                )

            [ID] => 489
        )

)

array_2

Array
(
    [0] => Array
        (
            [email] => Array
                (
                    [0] => test@domain.de
                )

            [ID] => 13
        )

    [1] => Array
        (
            [email] => Array
                (
                    [0] => mail@domain.de
                    [1] => yourmail@domain.de
                )

            [ID] => 48
        )

)

现在我想检查 array_2 中是否存在邮件地址“mail@domain.de”(array_1[0]['email'][0])。如果是:我需要知道找到邮件地址的 array_2 的密钥。

我试过 array_search() 但这似乎不适用于多数组。 你能帮我吗?谢谢!!

array_1 var_export()

array (
  0 => 
  array (
    'email' => 
    array (
      0 => 'mail@domain.de',
    ),
    'customerID' => '489',
  ),
)

array_2 var_export()

array (
  0 => 
  array (
    'email' => 
    array (
      0 => 'test@domain.de',
    ),
    'customerID' => '13',
  ),
  1 => 
  array (
    'email' => 
    array (
      0 => 'mail@domain.de',
      1 => 'yourmail@domain.de',
    ),
    'customerID' => '48',
  ),
)

您可以尝试使用 array_walk_recursive
一个粗略的例子:

$email = 'mail@domain.de'

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

array_walk_recursive($array2, 'checkEmail');

文档中有更多内容 https://www.php.net/manual/en/function.array-walk-recursive.php

由于您需要它所在位置的子数组索引,您可以

  • 在每个子数组上循环并调用搜索函数,例如 checkValueExistence 其中 return 是 truefalse 取决于搜索结果。如果 func returns true,return 索引,否则 -1false 表示找不到值(在您的情况下是电子邮件)。

  • checkValueExistence 将简单地遍历手头的数组并检查是否有任何值与提供的值匹配,如果上下文中的当前迭代值本身就是一个数组,它会递归地调用自身对于这个新的子阵列。

片段:

<?php
    
function getValueIndex($data, $val){
    foreach($data as $idx => $d){
        if(checkValueExistence($d, $val) === true){
            return $idx;
        }
    }
    
    return false;
    // you could also throw an exception via throw new \Exception("$val is not found in the dataset"); and a try-catch block in the callee code 
}

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

echo getValueIndex($data, 'mail@domain.de');

Online Demo

您可以将 array_filterarray_search 结合使用。如果找到数组,它会自动转换为布尔值 (true/false),从而返回父数组。

更新:array_search returns 值的索引:如果索引是 0,它被转换为 false,所以对 false 的严格类型检查应该修复了所有错误。

See it working over at 3v4l.org

array_filter($array, fn($x) => array_search('yourmail@domain.de', $x['email']) !== false);

输出:

Array
(
    [1] => Array
        (
            [email] => Array
                (
                    [0] => mail@domain.de
                    [1] => yourmail@domain.de
                )

            [customerID] => 48
        )

)

更新:如果 array_search 抛出意外结果,请尝试使用 in_array 代替。

array_filter($array, fn($x) => in_array('yourmail@domain.de', $x['email']));

更新:任何没有上下文的空结果,我假设它不是数组,实际上是一个字符串,因此您可以将检查附加到函数:

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