Search code examples
phparraysarray-push

add key and value to an existing array


I need to add key and value to an existing array and don't seem to be able to get it together.

My existing array when printed looks like this:

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
        )

)

I need to add a few details to this array on the fly from a string that looks like this:

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

Each entry is seperated by a new line and the name and email address is seperated by a :

So in the end my array would look like this:

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
        )

)

I looked at array_push but couldn't work it out.

Any help with this is very much sppreciated.

C


Solution

  • Using PHP Arrays' [] operator is the same as pushing a value to the end of an array.

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

    A full solution to your example would be

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

    which outputs

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