Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Arrays - Fatal编程技术网

Php 将列表操纵为关联数组

Php 将列表操纵为关联数组,php,arrays,Php,Arrays,我有以下变量$toEmails,其中包含3个电子邮件地址,用分号分隔。我想做的是将每个电子邮件地址放入一个数组,并在数组中添加另外两位信息,如“Name”和“to”变量 在线阅读时,我需要使用php explode命令将其拆分为一个数组,但是我对如何向数组中注入额外数据有点迷茫。我想我应该使用array_push命令来插入其他数据 这是我掌握的数据 $toEmails = "a@hotmail.com;b@yahoo.com;c@gmail.com"; $myName = "myName";

我有以下变量$toEmails,其中包含3个电子邮件地址,用分号分隔。我想做的是将每个电子邮件地址放入一个数组,并在数组中添加另外两位信息,如“Name”和“to”变量

在线阅读时,我需要使用php explode命令将其拆分为一个数组,但是我对如何向数组中注入额外数据有点迷茫。我想我应该使用array_push命令来插入其他数据

这是我掌握的数据

$toEmails = "a@hotmail.com;b@yahoo.com;c@gmail.com";
$myName = "myName";
这是我想要的输出

        array(
            'email' => 'a@hotmail.com',
            'name' => 'myName',
            'type' => 'to'
        ),
        array(
            'email' => 'b@yahoo.com',
            'name' => 'myName',
            'type' => 'to'
        ),
        array(
            'email' => 'c@gmail.com',
            'name' => 'myName',
            'type' => 'to'
        )

$arremails=爆炸(“;”,$toEmails)

您可以创建一个新数组并复制“分解”的电子邮件

或者使用引用的
foreach
循环更新数组中的值

$toEmails = "a@hotmail.com;b@yahoo.com;c@gmail.com";
$myName = "myName";

$arremails = explode(";", $toEmails);
$newArray = [];

foreach($arremails as &$email) {
    $email = [
        'email' => $email,
        'name' => $myName,
        'type' => 'to'
    ];
}

最短的解决方案是:

使用:

使用函数数组_map()()


在PHP5.4中,您可以使用[]代替array()。

问题是什么?分解字符串,然后得到一个电子邮件数组,循环这个数组,并在循环中创建新数组。
$toEmails = "a@hotmail.com;b@yahoo.com;c@gmail.com";
$myName = "myName";

$arremails = explode(";", $toEmails);
$newArray = [];

foreach($arremails as &$email) {
    $email = [
        'email' => $email,
        'name' => $myName,
        'type' => 'to'
    ];
}
$toEmails = "a@hotmail.com;b@yahoo.com;c@gmail.com";
$myName = "myName";
$arremails = [];

foreach(explode(';', $toEmails) as $email) {
    $arremails[] = [
        'email' => $email,
        'name' => $myName,
        'type' => 'to'
    ];
}
print_r($arremails);
$toEmails = "a@hotmail.com;b@yahoo.com;c@gmail.com";
$myName = "myName";

$emails = explode(';', $toEmails);
$output = array_map(
    function ($email) use ($myName) {
        return [
            'email' => $email,
            'name' => $myName,
            'type' => 'to',
        ];
    },
    $emails
);
$toEmails = "a@hotmail.com;b@yahoo.com;c@gmail.com";
$myName = "myName";

$result = array_map(function ($element) use ($myName) {
    return array('email' => $element, 'name' => $myName, 'type' => 'to');   
}, explode(';', $toEmails));

print_r($result);