There's two ways to do this. The first is to continue using your "hits:id" string in a single-dimensional array. Why, oh why, would you want to do this, though? It forces you to perform regular expression type matches on EVERY STRING in the array to extract the "hits" part.
Granted, since you're string is of the form <integer>:<integer>, you can use PHP's built-in string-to-integer conversion for the task. This conversion will work properly because a colon is NOT a valid character in a number. (Good thing you didn't choose to use a decimal point.)
The next thing you need to do is figure out how to remove an element from an array. That's easy -- unset().
But what you're really talking about doing is removing some undetermined SET of elements from an array using a fixed criteria. You can write a function to calculate this criteria, and pass the name of this function to one of the several array walking functions that filters an array based on the boolean result of a callback function.
Okay, that was more than likely over most people's heads, so lemme explain a few things:
A "callback" function is a function that you write that you don't explicitly call yourself -- rather, you tell some other function or object that you want them to call that function when it's appropriate for them.
"Walking" an array means to iterate over each element in an array and (typically) apply some processing on each element.
When you walk an array and just call a function on each element (this function is a callback function), this is called "mapping" a function onto an array.
Using that knowledge, we can see that we simply need to write a function that determines whether an element should be kept in the array or not.
function has_hits($str)
{
return ((int)$str) > 0);
}
This converts $str to an integer, and returns TRUE if that integer is greater than zero, false otherwise.
There are several functions available in PHP that perform walking, mapping, filtering, etc. Here's a list of them:
http://www.php.net/array_map
http://www.php.net/array_walk
http://www.php.net/array_reduce
http://www.php.net/array_filter
Judging by the names, it seems obvious that "array_filter" is probably the most applicable to our problem.
$hits_only = array_filter($matches, 'has_hits');
array_filter will run has_hits() on every element in the $matches array, and if has_hits() returns true, that element will be added to the result array.
A PHP implementation of array_filter() would be:
function array_filter(&$array, $func)
{
$ret = array();
foreach($array as $key => $val)
{
if ($func($val))
{
$ret[$key] = $val;
}
}
return $ret;
}
Okay, so now we're still left with the (imho) sloppy implementation of encoding both hits and id's in the same string. Why not use a nested array, with both 'hits' and 'id' indexes?
$matches SHOULD look like this:
(
[0] => Array
(
[hits] => 2
[id] => 12
)
[1] => Array
(
[hits] => 2
[id] => 11
)
[2] => Array
(
[hits] => 1
[id] => 19
)
[4] => Array
(
[hits] => 0
[id] => 5
)
[5] => Array
(
[hits] => 3
[id] => 9
)
)
has_hits() would then be implemented as:
function has_hits($match)
{
return $match['hits'] > 0;
}
Take care,
Nik
http://www.bigaction.org/