note 18359 deleted from function.array-splice by irc-html

From: Date: Sat, 19 Jan 2002 21:14:15 +0000
Subject: note 18359 deleted from function.array-splice by irc-html
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-25480@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.

« previous php.notes (#25480) next »