Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用输入数组中的键构造数组_Php - Fatal编程技术网

Php 使用输入数组中的键构造数组

Php 使用输入数组中的键构造数组,php,Php,我有一个数组,在一个逗号分隔的数组中有一个PID、电子邮件和客户端的列表。我想知道是否有办法解析输入数组,并生成一个新数组,该数组以“Email”作为键,并为该电子邮件生成所有唯一的PID。由于输入数组可以有数千个元素,我想知道最快的方法 Ex: Input Array (PID, Email, Client) -------------------------------------- Array ( [0] => 10, abc@test.com,Gmail [1] => 1

我有一个数组,在一个逗号分隔的数组中有一个PID、电子邮件和客户端的列表。我想知道是否有办法解析输入数组,并生成一个新数组,该数组以“Email”作为键,并为该电子邮件生成所有唯一的PID。由于输入数组可以有数千个元素,我想知道最快的方法

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