TheJoker
TheJoker

Reputation: 21

Get the key for a value in my array

I have an user array like this :

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 " 
    } 
} 

I Have also a rejected user array:

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


Expected Output : If I iterate my rejected user array, for example, for KENE Michael , It must be send " A057" (first occurence founded in user array )

How can I proceed ? I tried array_search but return nothing

My code

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

Upvotes: 1

Views: 71

Answers (2)

littleibex
littleibex

Reputation: 1712

This question is the continuation of your previous question. Based on that knowledge here is the answer to your 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;
}

Upvotes: 1

jde
jde

Reputation: 198

You have to iterate with foreach on your arrays and compare strings.

Upvotes: 0

Related Questions