2017-12-15 2 views
-1

Ich fange oft an, ein array zu definieren, und dann herauszufinden, ich muss es in zwei oder mehr Teile aufteilen. Ich würde dann mit $array('key' => 'value') für meine ersten Werte beginnen und dann den Code für die anderen Teile des Arrays wie folgt neu schreiben: $array['key'] = 'value'. Aber das ist ein Ärger.Array Merge auf dem Sprung

Also habe ich versucht, die folgende, die zu funktionieren scheint:

$my_true_love_sent_me_for_christmas = array(
    'First day'  => 'A Partridge in a Pear Tree', 
    'Second day' => '2 Turtle Doves', 
    'Third day'  => '3 French Hens', 
    'Fourth day' => '4 Calling Birds', 
); 

breathe(); // breathe, and then I want to continue my array where I left it. 

$my_true_love_sent_me_for_christmas = array_merge($my_true_love_sent_me_for_christmas, array(
    'Fifth day'  => '5 Golden Rings', 
    'Sixth day'  => '6 Geese a Laying', 
    'Seventh day' => '7 Swans a Swimming', 
    'Eighth day' => '8 Maids a Milking', 
)); 

breathe(); // breathe, and then I want to continue my array where I left it. 

$my_true_love_sent_me_for_christmas = array_merge($my_true_love_sent_me_for_christmas, array(
    'Ninth day'  => '9 Ladies Dancing', 
    'Tenth day'  => '10 Lords a Leaping', 
    'Eleventh day' => '11 Pipers Piping', 
    'Twelfth day' => '12 Drummers Drumming', 
)); 

Gibt es einen besseres/Reinigungsmittel/schnellerer Weg, auf dem Sprung ein Array Verschmelzung zu tun?

+0

Ich nehme an, Sie zuerst alle Felder definieren könnte und führen Sie dann nur eine 'array_merge()' am Ende vorbei alle Arrays in. – MCMXCII

+0

merge alle Array auf einmal ..! – Bhargav

+0

Warum der Downvote? Ich habe meine Nachforschungen angestellt, biete zwei mögliche Lösungen für das Problem an und stelle eine klare Frage, wie es verbessert werden kann. – OnklMaps

Antwort

2

Der einfachste Weg zu sein scheint:

$foo = [ 
    'First day' => 'A Partridge in a Pear Tree', 
    'Second day' => '2 Turtle Doves', 
    'Third day' => '3 French Hens', 
    'Fourth day' => '4 Calling Birds', 
]; 

$foo += [ 
    'Fifth day' => '5 Golden Rings', 
    'Sixth day' => '6 Geese a Laying', 
    'Seventh day' => '7 Swans a Swimming', 
    'Eighth day' => '8 Maids a Milking', 
]; 

$foo += [ 
    'Ninth day' => '9 Ladies Dancing', 
    'Tenth day' => '10 Lords a Leaping', 
    'Eleventh day' => '11 Pipers Piping', 
    'Twelfth day' => '12 Drummers Drumming', 
]; 

print_r($foo); 

Referenz: https://php.net/manual/language.operators.array.php

Der Operator + gibt das rechte Array zurück, das an das linke Array angehängt ist. Für Schlüssel, die in beiden Arrays vorhanden sind, werden die Elemente aus dem linken Array verwendet, und die übereinstimmenden Elemente aus dem rechten Array werden ignoriert.

+0

Ah .. Perfekt. In meinem Fall würde ich mit '$ foo = array (key => value) 'beginnen, 2/3 des Arrays abbrechen und mit' $ foo + = [key => value] 'fortfahren. Sehr sauber meiner Meinung nach! – OnklMaps

0

Ich glaube nicht, dass Sie es auf diese Weise komplizieren müssen. Das funktioniert auch. Auf jeden Fall eine kürzere Version aber:

<?php 
$my_true_love_sent_me_for_christmas = array(
    'First day'  => 'A Partridge in a Pear Tree', 
    'Second day' => '2 Turtle Doves', 
    'Third day'  => '3 French Hens', 
    'Fourth day' => '4 Calling Birds', 
); 

breathe(); // breathe, and then I want to continue my array where I left it. 

$my_true_love_sent_me_for_christmas['Fifth day']  = '5 Golden Rings'; 
$my_true_love_sent_me_for_christmas['Sixth day']  = '6 Geese a Laying'; 
$my_true_love_sent_me_for_christmas['Seventh day'] = '7 Swans a Swimming'; 
$my_true_love_sent_me_for_christmas['Eighth day'] = '8 Maids a Milking'; 

breathe(); // breathe, and then I want to continue my array where I left it. 

$my_true_love_sent_me_for_christmas['Ninth day']  = '9 Ladies Dancing'; 
$my_true_love_sent_me_for_christmas['Tenth day']  = '10 Lords a Leaping'; 
$my_true_love_sent_me_for_christmas['Eleventh day'] = '11 Pipers Piping'; 
$my_true_love_sent_me_for_christmas['Twelfth day'] = '12 Drummers Drumming'; 

Die Version, die als die oben besser ist es, alle Arrays irgendwo zu definieren und dann ein array_merge auf einmal.

$my_true_love_sent_me_for_christmas = $chrismas1; 

breathe(); // breathe, and then I want to continue my array where I left it. 

$my_true_love_sent_me_for_christmas = array_merge($my_true_love_sent_me_for_christmas, $chrismas2); 

breathe(); // breathe, and then I want to continue my array where I left it. 

$my_true_love_sent_me_for_christmas = array_merge($my_true_love_sent_me_for_christmas, $chrismas3); 
+0

Gibt es einen bestimmten Grund für eine Absage? Dies ist nur meine Meinung und bessere Version als die vorherige. –

+0

Ich habe bereits den '$ array ['key'] = 'value'-Weg erwähnt. Und wie definiert man das Array woanders, um es besser zu machen? Ich habe nach einer Möglichkeit gefragt, es unterwegs zu machen. – OnklMaps

+0

@OnklMaps Okay ... :) –

1

Dies ist schon sauber in meiner ehrlichen Meinung, aber wenn Sie den Code sauberer machen wollen, werfen Sie einen Blick auf, was wiederholt Schwierigkeiten verursacht. Definieren Sie eine Hilfsfunktion wie folgt aus:

function addRange(&$mainArray, $range) { 
    foreach ($range as $k => $v) { 
     $mainArray[$k] = $v; 
    } 
} 

Dann könnten Sie leicht Ihren Code vereinfachen:

$my_true_love_sent_me_for_christmas = array(
    'First day'  => 'A Partridge in a Pear Tree', 
    'Second day' => '2 Turtle Doves', 
    'Third day'  => '3 French Hens', 
    'Fourth day' => '4 Calling Birds', 
); 

breathe(); // breathe, and then I want to continue my array where I left it. 

addRange($my_true_love_sent_me_for_christmas, array(
    'Fifth day'  => '5 Golden Rings', 
    'Sixth day'  => '6 Geese a Laying', 
    'Seventh day' => '7 Swans a Swimming', 
    'Eighth day' => '8 Maids a Milking', 
)); 

breathe(); // breathe, and then I want to continue my array where I left it. 

addRange($my_true_love_sent_me_for_christmas, array(
    'Ninth day'  => '9 Ladies Dancing', 
    'Tenth day'  => '10 Lords a Leaping', 
    'Eleventh day' => '11 Pipers Piping', 
    'Twelfth day' => '12 Drummers Drumming', 
)); 
+0

Ich mag das. Es ist seltsam, dass dies nicht (?) Eine eingebaute Funktion für PHP ist. Muss eine gemeinsame Sache sein wollen. – OnklMaps

+0

@OnklMaps in der Tat, soweit ich weiß, das ist nicht eingebaut und ich stimme zu, das ist eine gemeinsame Sache, die man tun möchte. –