2012-09-18 88 views
4

我需要將鍵和值添加到現有數組,並且似乎無法將它們組合在一起。將鍵和值添加到現有數組

我現有的陣列打印時看起來像這樣:

Array 
(
    [0] => stdClass Object 
     (
      [id] => 787 
      [name] => Steve 
      [surname] => Ryan 
      [email] => [email protected] 
     ) 

    [1] => stdClass Object 
     (
      [id] => 1057 
      [name] => Peter 
      [surname] => Smith 
      [email] => [email protected] 
     ) 

    [2] => stdClass Object 
     (
      [id] => 1058 
      [name] => Chris 
      [surname] => Gill 
      [email] => [email protected] 
     ) 

) 

我需要在一些細節上飛從string,看起來像這樣添加到這個數組:

Topher:[email protected] 
Elvis:[email protected] 
Marilyn:[email protected] 

每個條目是由new line分開,名稱和電子郵件地址是由:

分離所以最後我的數組會看起來像這樣:

Array 
(
    [0] => stdClass Object 
     (
      [id] => 787 
      [name] => Steve 
      [surname] => Ryan 
      [email] => [email protected] 
     ) 

    [1] => stdClass Object 
     (
      [id] => 1057 
      [name] => Peter 
      [surname] => Smith 
      [email] => [email protected] 
     ) 

    [2] => stdClass Object 
     (
      [id] => 1058 
      [name] => Chris 
      [surname] => James 
      [email] => [email protected] 
     ) 
    [3] => stdClass Object 
     (
      [id] => 
      [name] => Topher 
      [surname] => 
      [email] => [email protected] 
     ) 
    [4] => stdClass Object 
     (
      [id] => 
      [name] => Elvis 
      [surname] => 
      [email] => [email protected] 
     ) 
    [5] => stdClass Object 
     (
      [id] => 
      [name] => Marilyn 
      [surname] => 
      [email] => [email protected] 
     ) 

) 

我看着array_push,但無法解決它。

任何與此有關的幫助是非常感謝。

Ç

+2

向我們展示你已經嘗試了什麼。數組推送應單詞或只是'$ array ['key'] = $ value' – Erik

+0

您可能正在尋找$ myarray [] = $ appendValue; 等效於JavaScript的myarray.push(appendValue); –

+0

謝謝@HunterF我會檢查一下。 – Cybercampbell

回答

2

使用PHP陣列[]操作是一樣推動一個值數組的末尾。

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

完整的解決方案,以你的例子是

$CharacterInput = <<<EOT 
Topher:[email protected] 
Elvis:[email protected] 
Marilyn:[email protected] 
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] => [email protected] 
) 

[1] => stdClass Object 
(
[id] => 
[name] => Elvis 
[surname] => 
[email] => [email protected] 
) 

[2] => stdClass Object 
(
[id] => 
[name] => Marilyn 
[surname] => 
[email] => [email protected] 
) 

) 
-2

假設$ ARR是現有的數組變量,做如下說明。

$arr[] = array(
      ['id'] => '', 
      ['name'] => 'Topher', 
      ['surname'] => '', 
      ['email'] => '[email protected]' 
); 
+0

謝謝@Salsan - 這給了我錯誤。 – Cybercampbell

+0

對不起,我忘了添加單引號字符串... –

+0

它應該現在工作...嘗試... –

3

你可以嘗試這樣的事情

$string = "Topher:[email protected]\nElvis:[email protected]\nMarilyn:[email protected]"; 
$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] => [email protected] 
      [name] => Topher 
     ) 

    [1] => stdClass Object 
     (
      [id] => 
      [surname] => 
      [email] => [email protected] 
      [name] => Elvis 
     ) 

    [2] => stdClass Object 
     (
      [id] => 
      [surname] => 
      [email] => [email protected] 
      [name] => Marilyn 
     ) 

) 
+0

謝謝...這是完美的! – Cybercampbell