Add key and value to existing array

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

My existing array when printed is as follows:

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 some details to this array on the fly from string , which looks like this:

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

Each entry is separated by the new line symbol, and the name and email address are separated by the symbol :

So at the end, my array will 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 handle it.

Any help on this is very much lit.

WITH

+4
source share
3 answers

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

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

A complete solution for 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 ) ) 
+2
source

You can try something like this

 $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 Code Example

Result:

 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 ) ) 
+3
source

Assuming $ arr is your existing array variable, do it as follows.

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

Source: https://habr.com/ru/post/1434734/


All Articles