www.webdeveloper.com
Results 1 to 8 of 8

Thread: Another array question

  1. #1
    Join Date
    Oct 2005
    Location
    California
    Posts
    603

    Another array question

    Is there a way to extract a portion of an array that only contains certain values, without having to loop through the entire thing?

    For instance, if I have an array that looks like this:

    $arr[]=array('a', 'w');
    $arr[]=array('b', 'w');
    $arr[]=array('c', 'x');
    $arr[]=array('d', 'y');
    $arr[]=array('e', 'w');

    I want to extract an array that looks like this:

    $arr2[]=array('a', 'w');
    $arr2[]=array('b', 'w');
    $arr2[]=array('e', 'w');

    effectively, limiting the second array to records that have 'w' in the second index.

    It's much like using a where statement in a sql query.

    Any idea how to do this without having to loop through foreach?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,926
    Untested:
    PHP Code:
    $arr2 array_filter($arrcreate_function('$a''return($a[1]=="w");')); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2007
    Location
    Tangerang, Banten
    Posts
    258
    tested :
    PHP Code:
    <?php
    $arr
    []=array('a''w');
    $arr[]=array('b''w');
    $arr[]=array('c''x');
    $arr[]=array('d''y');
    $arr[]=array('e''w');
    function 
    tester($n)
    {
        if(
    $n[1]=='w')
        {
            return 
    $n;
        }
    }
    $arr2 array_filter($arr,"tester");
    //$arr2 = array_filter($arr, create_function('$a', 'return($a[1]=="w");')); -> same output
    //if you care about index
    sort($arr2);
    print_r($arr2);
    ?>
    It will output :
    Array ( [0] => Array ( [0] => a [1] => w ) [1] => Array ( [0] => b [1] => w ) [2] => Array ( [0] => e [1] => w ) )

  4. #4
    Join Date
    Oct 2005
    Location
    California
    Posts
    603
    NogDog, Thank you!

    $arr2 = array_filter($arr, create_function('$a', 'return($a[1]=="w");'));

    is exactly what I needed! You did say it was untested, but I should add–for others searching for a similar answer–that the first parameter, '$a', only works without the single quotes.

    andre4s_y, thank you as well for:

    $arr2 = array_filter($arr,"tester");

    The way you broke it down with the separate callback function helped me understand how the array_filter function worked. However, since I'm using it within a class, I couldn't figure out how to use the callback function within the class. For instance:

    $arr2 = array_filter($arr, $this->"tester");
    or
    $arr2 = array_filter($arr, "$this->tester");

    didn't work. This is an issue that I've had with callback functions before. Do you (or anyone else) know how to use this inside of a class?

  5. #5
    Join Date
    Sep 2006
    Location
    Bucharest, RO
    Posts
    940
    Quote Originally Posted by sitehatchery
    I couldn't figure out how to use the callback function within the class.
    http://php.net/callback
    Basically: array_filter($arr, Array($this, "tester")); or: array_filter($arr, Array("ClassName", "tester"));


  6. #6
    Join Date
    Oct 2005
    Location
    California
    Posts
    603
    Aha! THanks, In the past, I've looked for it on PHP.net, did google searches and have even posted threads here on that. Don't know why I always came up short of the answer. Thanks for your response!

  7. #7
    Join Date
    Oct 2005
    Location
    California
    Posts
    603
    Well, NogDog... you were right. You do need the single quotes around the $a. When I didn't include it, I mistakenly thought it worked because it returned all rows.

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,926
    Quote Originally Posted by sitehatchery
    Well, NogDog... you were right....
    Was there ever any doubt?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles