0

Hello,

I want array in format

$xyz[]=array($key=>'Some value here',$value=>array('value1','value2));

In below code,
I want it in loop by checking if $ManagerGSID value is not in $xyz[] as $key then insert into it. (as key will $ManagerGSID and value will be array ($EmployeeGSID))
again if got the same $ManagerGSID then push $EmployeeGSID to the last of array ($EmployeeGSID,$EmployeeGSID)

But if $ManagerGSID is not in $xyz array then make new entry of it in xyz as key= $ManagerGSID and value as array ($EmployeeGSID)

But how to do it?

    $managers=array();
    //$xyz[]=array($key=>'',$value=>array()); 
    foreach($Skills as $_Skills)
    {
        $EmployeeGSID=$_Skills['uid'];

        //fetch Manager of employee
        $ManagerGSID=helpers::getManager($EmployeeGSID);
        //push manager id in array if it is not already in array
        if (!in_array($ManagerGSID, $managers))
        {
            array_push($managers,$ManagerGSID);
            //array_push($xyz,$key=>$ManagerGSID,$value=>array_merge($value,$EmployeeGSID)); // want here like this
        }
    }
2
Contributors
2
Replies
27
Views
3 Years
Discussion Span
Last Post by Priti_P
0

Doesn't seem to make much sense to me:

The array setup you want is a straight two item array - a scalar value (key) and then an array of variable length ($value) for each $xyz item.

I'm not sure why you just don't have this structure...

$xyz[$key] = array(...);

For that you could just do:

if(array_key_exists($keyToCheck, $xyz))
{
    if(!in_array($valueToCheck, $xyz[$keyToCheck]))
        $xyz[$keyToCheck][] = $valueToCheck;
}else{
    $xyz[$keyToCheck] = array($valueToCheck);
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.