php - 将键和值添加到现有数组

标签 php arrays array-push

我需要将键和值添加到现有数组,但似乎无法将它们组合在一起。

打印时我现有的数组如下所示:

Array
(
    [0] => stdClass Object
        (
            [id] => 787
            [name] => Steve
            [surname] => Ryan
            [email] => Steve@hotmail.com
        )

    [1] => stdClass Object
        (
            [id] => 1057
            [name] => Peter
            [surname] => Smith
            [email] => Peter.Smith@yahoo.com
        )

    [2] => stdClass Object
        (
            [id] => 1058
            [name] => Chris
            [surname] => Gill
            [email] => chrisgill@gmail.com
        )

)

我需要从一个 string 中动态地向这个数组添加一些细节,如下所示:

Topher:Topher1234@mac.com
Elvis:elvispresley@gmail.com
Marilyn:marilyn.monroe@hotmail.com

每个条目由新行分隔,姓名和电子邮件地址由分隔:

所以最后我的数组看起来像这样:

Array
(
    [0] => stdClass Object
        (
            [id] => 787
            [name] => Steve
            [surname] => Ryan
            [email] => Steve@hotmail.com
        )

    [1] => stdClass Object
        (
            [id] => 1057
            [name] => Peter
            [surname] => Smith
            [email] => Peter.Smith@yahoo.com
        )

    [2] => stdClass Object
        (
            [id] => 1058
            [name] => Chris
            [surname] => James
            [email] => chrisjames@gmail.com
        )
    [3] => stdClass Object
        (
            [id] => 
            [name] => Topher
            [surname] => 
            [email] => Topher1234@mac.com
        )
    [4] => stdClass Object
        (
            [id] => 
            [name] => Elvis
            [surname] => 
            [email] => elvispresley@gmail.com
        )
    [5] => stdClass Object
        (
            [id] => 
            [name] => Marilyn
            [surname] => 
            [email] => marilyn.monroe@hotmail.com
        )

)

我查看了 array_push 但无法解决。

非常感谢任何帮助。

C

最佳答案

你可以试试这样的东西

$string = "Topher:Topher1234@mac.com\nElvis:elvispresley@gmail.com\nMarilyn:marilyn.monroe@hotmail.com";
$string = explode("\n", $string);
$result = array(); # or your existing array
foreach($string as $chunk){
        $to_array = new stdClass();
        $to_array->id = null;
        $to_array->surname = null;
        list($to_array->name, $to_array->email) = explode(':', $chunk);
        $result[] = $to_array;
}
print_r($result);

Codepad sample

结果:

Array
(
    [0] => stdClass Object
        (
            [id] => 
            [surname] => 
            [email] => Topher1234@mac.com
            [name] => Topher
        )

    [1] => stdClass Object
        (
            [id] => 
            [surname] => 
            [email] => elvispresley@gmail.com
            [name] => Elvis
        )

    [2] => stdClass Object
        (
            [id] => 
            [surname] => 
            [email] => marilyn.monroe@hotmail.com
            [name] => Marilyn
        )

)

关于php - 将键和值添加到现有数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12469037/

相关文章:

php - laravel Auth::login($user) 总是返回 401 Unauthorized

C - 结构和列表

c++ - 类型 'const char*' 的参数与类型 'char*' 的参数不兼容

javascript - 如何计算javascript中数组对象的唯一值

php - 检查数据库和/或表是否存在

php - 使用 PHP 连接到 IMAP

php - 回调期间在类函数中调用外部对象

c++ - 指向更大 unsigned char 数组的每个第 n 个元素的指针数组

javascript - 将对象插入数组会产生相同的值

javascript - 我如何从具有混合值的较大数组返回具有相等值的多个数组?