将键和值添加到现有数组

时间:2012-09-18 00:55:27

标签: 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

每个条目都由new line分隔,名称和电子邮件地址由:

分隔

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

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

3 个答案:

答案 0 :(得分:3)

您可以尝试这样的事情

$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
        )

)

答案 1 :(得分:2)

使用PHP Arrays'[]运算符与将值推送到数组末尾相同。

$arr[] = 'new element in array';

您示例的完整解决方案是

$CharacterInput = <<<EOT
Topher:Topher1234@mac.com
Elvis:elvispresley@gmail.com
Marilyn:marilyn.monroe@hotmail.com
EOT;

$lines = explode("\n", $CharacterInput);

$People = array();

foreach($lines as $line) {
    list($name, $email) = explode(':', $line);

    $entry = array(
        'id' => null,
        'name' => $name,
        'surname' => null,
        'email' => $email,
    );

    // cast the entered data array to an object,
    // which is explicitly stated in the example output.
    // For plain arrays, remove the following line.
    $entry = (object) $entry;

    $People[] = $entry;
}

print_r($People);

输出

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

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

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

)

答案 2 :(得分:-2)

假设$ arr是您现有的数组变量,请按照下面的说明执行。

$arr[] =  array(
            ['id'] => '',
            ['name'] => 'Topher',
            ['surname'] => '',
            ['email'] => 'Topher1234@mac.com'
);