PDA

View Full Version : how to filter an array


piz
08-24-2002, 05:26 PM
Hi!

How you can filter an array to delete for Example all entries with "none"?

How does the function array_filter exactly work? Was is callback?

The only thing I know is that using the function array_filter without callback-parameter you filter all the empty entries.

(And how it is possible to delete one entrie in an array?)

Thx.

piz

mordred
08-24-2002, 08:17 PM
A small example to illustrate the use of a callback function and array_filter():


function isNone($var) {
return (strtolower(trim($var)) != 'none');
}

$array3 = array("hello", "world", "drink", "none", "more", "wine", "none");

$array3 = array_filter($array3, "isNone");
var_dump($array3);


To delete/remove elements from an array, you can make use of either unset() (http://us.php.net/manual/en/function.unset.php) or array_splice() (http://us.php.net/manual/en/function.array-splice.php).

piz
08-24-2002, 09:07 PM
Thx!!!
nice array... ;-)

another question.

What exactly is a callback?

It is possible to do it like this, too?

$array3 = array_filter($array3, !="none");


Or something similar?

Thx.