note 18359 added to function.array-splice

From: Date: Sat, 19 Jan 2002 20:24:26 +0000
Subject: note 18359 added to function.array-splice
Groups: php.notes 
Request: Send a blank email to php-notes+get-25298@lists.php.net to get a copy of this message
I had an array generated by mysql_fetch_row() and I needed to filter out values which were == ''. This was fairly easy with: $num = count($mysqlarray); for ($i = 0; $i < $mysqlarray; $i++) { if ($mysqlarray[$i] == '') { unset($mysqlarray[$i]); } } but this left gaps in the keys (ie: 1=>1, 3=>3, 4=>4). array_splice() automatically resequences the keys for you, but that's the only function I know of which will do it, and you can't have that functionality without splicing something. I wrote a small function: function array_compress ($array) { $temp = $array; while (list($key,$value) = each($array)) { $temp[] = $value; } return $temp; } This function takes an array and rewrites it maintaining the order but with sequential keys. It would be nice if something like this could be added as a PHP native function. -- http://www.php.net/manual/en/function.array-splice.php http://master.php.net/manage/user-notes.php?action=edit+18359 http://master.php.net/manage/user-notes.php?action=delete+18359 http://master.php.net/manage/user-notes.php?action=reject+18359

« previous php.notes (#25298) next »