SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Location
    Ljubljana
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cleaning the array

    Hello.

    I have a simple array with values and keys:

    $test = array(
    'key1' => 'some value',
    'key2' => 'some value..',
    'random_key' => '...'
    );

    and another array $variable = array('key1', 'random_key');

    I want to process the $test array so that it would only hold keys from the $variable array (without 'key2' item). I could do this manually by looping through the $test array but I've been wondering if theres any more elegant way to do this with php (maybe some existing function for this)?

    Thanks a lot for help!

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    See array_intersect(), that should be what you're looking for.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    array_intersect_key might be more useful.

    PHP Code:
    $filtered array_intersect_key($testarray_flip($variable)); 
    Salathe
    Software Developer and PHP Manual Author.


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
  •