note 105968 added to function.array-merge

From: Date: Wed, 28 Sep 2011 22:01:50 +0000
Subject: note 105968 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-183513@lists.php.net to get a copy of this message
I always confuse which does what and when. So I made this cheatsheet that you may find useful. The existing examples show some situations but not all of these four. <?php $n1 = array('foo', 'bar', 'baz'); $n2 = array('lorem', 'ipsum', 'foo'); // array plus: // - duplicate numeral keys are ignored, not overwriting // - new keys inserted at the end $n1 + $n2; ?> array(3) { [0]=> string(3) "foo" [1]=> string(3) "bar" [2]=> string(3) "baz" } <?php // array_merge: // - duplicate numeral keys are appended and keys are reindexed // - new keys inserted at the end array_merge( $n1, $n2 ); ?> array(6) { [0]=> string(3) "foo" [1]=> string(3) "bar" [2]=> string(3) "baz" [3]=> string(5) "lorem" [4]=> string(5) "ipsum" [5]=> string(3) "foo" } <?php $a1 = array('f' => 'foo', 'b' => 'bar', 'q' => 'quux'); $a2 = array('l' => 'lorem', 'i' => 'ipsum', 'f' => 'facilisis'); // array plus: // - duplicate associative array keys ignored, not overwriting // - new keys inserted at the end $a1 + $a2; ?> array(5) { ["f"]=> string(3) "foo" ["b"]=> string(3) "bar" ["q"]=> string(4) "quux" ["l"]=> string(5) "lorem" ["i"]=> string(5) "ipsum" } <?php // array plus: // - replacements are done at the original position // - new keys inserted at the end array_merge( $a1, $a2 ) ?> array(5) { ["f"]=> string(9) "facilisis" ["b"]=> string(3) "bar" ["q"]=> string(4) "quux" ["l"]=> string(5) "lorem" ["i"]=> string(5) "ipsum" } ---- Server IP: 69.147.83.197 Probable Submitter: 82.74.192.60 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- https://master.php.net/note/edit/105968 Del: integrated -- https://master.php.net/note/delete/105968/integrated Del: useless -- https://master.php.net/note/delete/105968/useless Del: bad code -- https://master.php.net/note/delete/105968/bad+code Del: spam -- https://master.php.net/note/delete/105968/spam Del: non-english -- https://master.php.net/note/delete/105968/non-english Del: in docs -- https://master.php.net/note/delete/105968/in+docs Del: other reasons-- https://master.php.net/note/delete/105968 Reject -- https://master.php.net/note/reject/105968 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#183513) next »