我有一阵 which。
$dataArray = array (
0 =>
array (
UserId => 804023 ,
ProjectCode => RA1234 ,
Role => PI ,
),
1 =>
array (
UserId => 804023 ,
ProjectCode => RA1234 ,
Role => PM ,
),
2 =>
array (
UserId => 804023 ,
ProjectCode => A90123 ,
Role => CI ,
),
3 =>
array (
UserId => 804023 ,
ProjectCode => A20022 ,
Role => PM ,
),
)
我需要这样做。
$expected = array (
804023 =>
array (
RA1234 =>
array (
0 => PI ,
1 => PM ,
),
A90123 =>
array (
0 => PI ,
),
A20022 =>
array (
0 => CI ,
),
),
)
我认为,这可以通用地利用再次入侵来实现,因为这是一个我可能多次到来的情景。
我已经走过了这么多的路程,这些钥匙构成nes阵列的关键。
$keys=array("UserId","projectCode","Role");
但是,看不到从哪里走到哪里?
public function structureData(array $data, array $keys)
{
//$structuredData = array();
foreach ($data as $key => $value)
{
$keyForData = array_slice($keys,0,1);
$remainingKeys = $keys;
array_shift($remainingKeys);
if (!array_key_exists($value[$keyForData[0]], $structuredData))
{
$count=count($remainingKeys);
$structuredData[$value[$keyForData[0]]] =array();
// this returns as expected array(804023 =>array ()); but subsequent recursive calls with the remaining data fail
}
}
return $structuredData);
}