Latest: Genstatic, my first sip of coffee

Content with Style

Web Technique

php: array_filter and callback function

by Pascal Opitz on April 20 2006, 09:48

Sometimes array_pop is just not enough. You might have to strip out something from an array that is 2 dimensional or do some more complicated adjustments to the array values. In this case you can use array_filter and a callback function.

The following example is class based:


<?
class test
{
  function test()
  {
    $arr = array();
    $arr[] = array('ID' => 1, 'name' => 'first');
    $arr[] = array('ID' => 2, 'name' => 'second');

    print_r($arr);
    $arr = array_filter($arr, array(&$this, "filter"));  
    print_r($arr);
  }

  function filter($var)
  {
    return($var['name'] != 'first');
  }
}

$test = new test();
?>

Comments

  • thanx-a-lot, pascal. a few days ago i had quite a bit of troublewhen i tried to define a class-based callback function for array_filter.

    is this definition also working in php5?

    by fanatique on April 20 2006, 16:06 - #

  • Glad I could help. This should do:
    $arr = array_filter($arr, array("self", "filter"));
    

    by Pascal Opitz on April 21 2006, 02:33 - #

  • Hi there. I’m currently working with an big array, and i have to filter some info out of it. This tutorial really helps, but i neat some more info.

    The array is very multidimentional, like this:
    $array[‘id’][‘category’][‘element’] = value.

    So i want to filter out all the ID’s wich don’t have the good Value in [‘category’][‘element’], how can i do this?

    couse the callback function only has $var, i cant give all the $ID’s:
    function filter($var)
    {
    return($var[’$id’] == ’$askedvalue’);
    }

    would work, but how can i add $id/$askedvalue in the array_filter($array, ‘callbackfunction’)?

    by Foleor on November 4 2006, 14:16 - #

  • How about setting the variables as class members or globals?

    by Pascal Opitz on November 6 2006, 11:06 - #

  • Hi thanks for this tut, I really need it, but I’m having trouble understanding the array filter. I need to make a loop from an array which calls in a simple flat file database for a site menu, the database contains the site menu link names i.e. my web page, and I need the array to filter that data stripping the spaces to return mywebpage.txt (where the actual page is) as well as pull in the link name, all within an li tag so the “my web page” shows up as the link name and the mywebpage.txt shows up as the link. Here’s my function

    function filter_str($string, $page=’’)
    {
    if ($page == ‘’)
    $filter = ereg_replace(’[^a-zA-Z0-9_]’, ‘’, $string);

    else
    $filter = ereg_replace(”[^a-zA-Z0-9_$page]”, ‘’, $string);

    return $filter;
    }

    $page = filter_str($page);

    and my array loop

    $array = file(“menudata.txt”);//holds menu link names

    foreach($array as $page) {

    echo “li a href=”index.php?page=$page”{$page}/a/li>”;

    (i had to take the link lesser/greater tags out in this post to get them to show up.)

    for the life of me I cannot figure out how to make it work.
    Thanks for any help you have time to provide.

    Karen

    by Karen on January 6 2008, 10:12 - #


Comments for this article are closed.