Php 使用输入数组中的键构造数组
我有一个数组,在一个逗号分隔的数组中有一个PID、电子邮件和客户端的列表。我想知道是否有办法解析输入数组,并生成一个新数组,该数组以“Email”作为键,并为该电子邮件生成所有唯一的PID。由于输入数组可以有数千个元素,我想知道最快的方法Php 使用输入数组中的键构造数组,php,Php,我有一个数组,在一个逗号分隔的数组中有一个PID、电子邮件和客户端的列表。我想知道是否有办法解析输入数组,并生成一个新数组,该数组以“Email”作为键,并为该电子邮件生成所有唯一的PID。由于输入数组可以有数千个元素,我想知道最快的方法 Ex: Input Array (PID, Email, Client) -------------------------------------- Array ( [0] => 10, abc@test.com,Gmail [1] => 1
Ex: Input Array (PID, Email, Client)
--------------------------------------
Array (
[0] => 10, abc@test.com,Gmail
[1] => 11, def@test.com,Gmail
[2] => 12, abc@test.com,Gmail
[3] => 13, def@test.com,Gmail
[4] => 14, xyz@test.com,Gmail
[5] => 15, def@test.com,Gmail
)
Ex: Output Array (with Email as the key):
---------------------------------------------
Array (
[abc@test.com] => (
[0] => 10
[1] => 12
),
[def@test.com] => (
[0] => 11
[1] => 13
[2] => 15
),
[xyz@test.com] => (
[0] => 14
)
)
谢谢我能想到的唯一办法是:
$outputArray = array();
foreach ($inputArray as $value)
{
list($pid, $email) = explode(",", trim($value));
$outputArray[$email][] = $pid;
}
我唯一能想到的是:
$outputArray = array();
foreach ($inputArray as $value)
{
list($pid, $email) = explode(",", trim($value));
$outputArray[$email][] = $pid;
}
foreach()然后explode()在逗号上逗号分隔的数组来自何处?您是在CSV文件上调用
fgets()
吗?foreach()然后在comma上explode()逗号分隔的数组来自何处?您正在对CSV文件调用fgets()
?
$outputArray = array();
foreach ($inputArray as $value)
{
list($pid, $email) = explode(",", trim($value));
$outputArray[$email][] = $pid;
}