获取数组

时间:2015-05-05 14:17:21

标签: php arrays matching username

我有一个用户数组,如下所示:

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但没有返回任何内容

我的代码

$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)

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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