4

キーと値を既存の配列に追加する必要がありますが、それらをまとめることができないようです。

印刷時の既存の配列は次のようになります。

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

4

3 に答える 3

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

コードパッドのサンプル

結果:

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
        )

)
于 2012-09-18T01:11:27.813 に答える
2

PHP配列の[]演算子を使用することは、値を配列の最後にプッシュすることと同じです。

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

)
于 2012-09-18T01:15:44.203 に答える
-2

$ arrが既存の配列変数であると仮定し、以下のように実行します。

$arr[] =  array(
            ['id'] => '',
            ['name'] => 'Topher',
            ['surname'] => '',
            ['email'] => 'Topher1234@mac.com'
);
于 2012-09-18T01:04:24.627 に答える