SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extract elements from an array which have a common substring in the key names

    Hi

    My problem is easily stated, but I can't figure out how to do it.

    a $_POST array has the following elements :-

    first-->'Ann Other', price-->21, chk_21-->21, chk_x-->24, chk_19-->19, converting-->yes, chk_37-->37

    I want to pull out all elements with names containing the "chk" substring into another array:-

    $index = array(chk_21-->21, chk_x-->24, chk_19-->19, chk_37-->37)

    Can you help me?

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Use preg_match to find the keys and place them in the new array($index) with the corresponding values.
    Here is an example:
    Code:
    <?php
       if(sizeof($_POST))
       {
    	   echo "<pre>";
    	   print_R($_POST);
    	   $index = array();
    	   foreach($_POST as $key => $val)
    	   {
    		   if(preg_match("/^chk/", $key)) $index[$key] = $val;
    	   }
    	   print_R($index);
    	   
    	   exit;
       }
       echo '
    <form method="post" action="extract_keys.php"><br/>
    first: <input type="text" name="first" value="Ann Other" /><br/>
    price: <input type="text" name="price" value="21" /><br/>
    chk_21: <input type="text" name="chk_21" value="21" /><br/>
    chk_x: <input type="text" name="chk_x" value="24" /><br/>
    chk_19: <input type="text" name="chk_19" value="19" /><br/>
    converting: <input type="text" name="converting" value="yes" /><br/>
    chk_37: <input type="text" name="chk_37" value="37" /><br/>
    <input type="submit" value="Send" /><br/>
    </form>
    ';
    ?>

  3. #3
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,904
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    If like me you do not like using preg_match() try this:

    PHP Code:

    $index
    =array();

    foreach( 
    $_POST as $key => $value):

      if (
    'chk' === substr($key3) )
      {
         
    $new[$key] = $value;
      }    

    endforeach;

    echo 
    '<pre>';    
      
    print_r$index );
    echo 
    '</pre>'
    Output:

    Array
    (
    [chk_21] => 21
    [chk_x] => 24
    [chk_19] => 19
    [chk_37] => 37
    )
    Last edited by John_Betong; Apr 16, 2012 at 06:57. Reason: sspelling: not my fortay
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  4. #4
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi tom8 & John_Betong - pretty neat....

    I am most grateful to both of you - where did you ever find these tricks. I had always assumed that a variable's name couldn't be changed

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,904
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    @chelse

    I found the tricks from various forums.

    I am not sure what you mean by a "variable's name couldn't be changed".

    I checked my code and noticed that the online editing had an error so here is the correct script:

    PHP Code:


    $index
    =array(); 

    foreach( 
    $_POST as $key => $value): 

      
    # I want to pull out all elements with names containing the "chk" substring into another array
      
    if ('chk' === substr($key3) ) 
      { 
         
    $index[$key] = $value
      }     

    endforeach; 

    echo 
    '<pre>';     
      
    print_r$index ); 
    echo 
    '</pre>'
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014


Bookmarks

Posting Permissions

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