note 96220 added to function.array-diff

From: Date: Mon, 15 Feb 2010 22:45:53 +0000
Subject: note 96220 added to function.array-diff
Groups: php.notes 
Request: Send a blank email to php-notes+get-165987@lists.php.net to get a copy of this message
array_diff does not create a new array containing the values present in $array1 but not $array2 -- it simply unsets all of the value in $array1 that are in $array2 and returns it. This means that the indexes of the returned array are typically not continuous from zero. For example: <?php $array1 = array('red', 'blue', 'green', 'octarine'); $array2 = array('red', 'yellow', 'green'); $diff = array_diff($array1, $array2); print_r($diff); //Array ( [1] => blue [3] => octarine ) ?> The easiest way around this seems to be use array_merge: <?php print_r(array_merge($diff)) //Array ( [0] => blue [1] => octarine ) ?> ---- Server IP: 92.48.74.199 Probable Submitter: 82.15.17.129 ---- Manual Page -- http://www.php.net/manual/en/function.array-diff.php Edit -- https://master.php.net/note/edit/96220 Del: integrated -- https://master.php.net/note/delete/96220/integrated Del: useless -- https://master.php.net/note/delete/96220/useless Del: bad code -- https://master.php.net/note/delete/96220/bad+code Del: spam -- https://master.php.net/note/delete/96220/spam Del: non-english -- https://master.php.net/note/delete/96220/non-english Del: in docs -- https://master.php.net/note/delete/96220/in+docs Del: other reasons-- https://master.php.net/note/delete/96220 Reject -- https://master.php.net/note/reject/96220 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165987) next »