获取数组中某个值的键

Get the key for a value in my array

我有一个 用户数组 是这样的:

array(3) { 
    ["A057"] => array(7) { 
        [0] => string(13) "KENE Michael " 
        [1]=> string(16) "KIRAN Maria " 
        [2]=> string(14) "ATHISA KATHISA " 
        [3]=> string(16) "SATYA Dev " 
        [4]=> string(16) "Raghav Laurent " 
        [5]=> string(12) "DARTY Suneep " 
        [6]=> string(22) "GIO Simral " 
    } 
    ["A154"]=> array(0) { } 
        [0]=> array(1) { 
          [0]=> string(17) "NICOLE Lotta " 
    } 
} 

我还有一个被拒绝的用户数组

array(3) { 
    [0]=> string(5) "KENE" 
    [1]=> string(4) "KIRAN" 
    [2]=> string(6) "ATHISA" 
}


预期输出: 如果我迭代我被拒绝的用户数组,例如,对于 KENE Michael ,它必须发送“A057”(在用户数组中首次出现)

我该如何进行? 我尝试了 array_search 但 return 没有

我的代码

$arrayListeAgence = array();
$usersAdded = array();
$arrayDupplicate = array();
for ($i = 0; $i < count($pieces); $i = $i + 2) {
$key = $pieces[$i];
$usersStr = $pieces[$i + 1];
$users = explode('/', $usersStr);
$arrayListeAgence[$key] = array();

for ($j = 0; $j < count($users); $j = $j + 2) {
    $username = $users[$j];
    if (array_search($username, $usersAdded) === false) {
        $arrayListeAgence[$key][] = $username . " " . $users[$j + 1] . " ";
        $usersAdded[] = $username;
    }
    else {
        /*foreach($arrayListeAgence as $key => $val) {
            print($key); // 
            //print_r($val); //
        }*/
        if (array_search($username, $arrayListeAgence) === false) {
            //if we found user in arrayListAgence
            $arrayDupplicate[] = $username.$key;
        }

    }
}

}

var_dump($arrayDupplicate);//$key equal to A154 (I want A057-first occurence found on the main array)

您必须在数组上使用 foreach 进行迭代并比较字符串。

这个问题是你之前的延续。基于这些知识,这里是您问题的答案:

function getAgentForRejectedUser($rejectedUser, $arrayListeAgence)
{
    foreach ($arrayListeAgence as $agent => $users) {
        foreach ($users as $user) {
            $data = explode(' ', $user);
            if ($data[0] == $rejectedUser) {
                return $agent;
            }
        }
    }
    return false;
}