Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Finding vars

  1. #1
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts

    Finding vars

    Ok here's the deal ha ha. I have this script that receives GET parameters in the form of:

    item_number1 = Download
    item_number2 = Tangible
    But I have no idea of knowing at run time how many item_numerX there are. I have to go through each one and do something with it. So my question is, how do I find out how many of those item_numbers there are in the GET sequence to know how many times I have to go through it? Or do I just go until it breaks?

    Thanks for any help in this matter.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Here's something I cam up with a while back on another forum:
    http://www.hotscripts.com/forums/php...wildcards.html

    Also see: http://www.php.net/preg_grep for further details. All this does is run preg_grep() on the array keys as opposed to the values.

    PHP Code:
    function preg_grep_keys$pattern$input$flags )
    {
        
    $keys preg_grep$patternarray_keys$input ), $flags );
        
    $vals = array();
        foreach ( 
    $keys as $key )
        {
            
    $vals[$key] = $input[$key];
        }
        return 
    $vals;

    PHP Code:
    $matches preg_grep_keys'/^item_number\d+$/'$_GET );

    count$matches ); // # of matching keys from array 
    $matches is an associative array of the matching keys and their values, so you can just iterate that instead.
    Last edited by kbluhm; 01-22-2010 at 12:37 AM.

  • #3
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Perfect just what I was looking for, thanks. I didn't know your could preg match with variable names.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Well, not natively... hence the custom function.


  •  

    Posting Permissions

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