SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    remove elements from array equal to

    PHP Code:
    [0]=>
      array {
        
    "id" => 11,
        
    "rule"=> "rule 1"
      
    },
    [
    1]=>
      array {
        
    "id" => 40,
        
    "rule"=> "rule 2"
      
    }
    [
    3]=>
      array {
        
    "id" => 55,
        
    "rule"=> "rule 3"
      
    }
    [
    4]=>
      array {
        
    "id" => 40,
        
    "rule"=> "rule 4"
      


    How am I able to unset the elements with id = 40 to get the following array?

    PHP Code:
    [0]=>
      array {
        
    "id" => 11,
        
    "rule"=> "rule 1"
      
    },
    [
    1]=>
      array {
        
    "id" => 55,
        
    "rule"=> "rule 3"
      

    Do I need to do a foreach cycle / array check / unset?
    Or is there any php function for it?

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    I have a feeling this is wrong as I am very rusty with PHP, but the right direction that you or someone else can finish off?

    PHP Code:
    foreach ($array as $rule) {
    if 
    $rule['rule'] = "rule 2' then {
    unset(
    $rule['id']);
    }


  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,121
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pedroz View Post
    Do I need to do a foreach cycle / array check / unset?
    Or is there any php function for it?
    I've been thinking this one over for a few hours, and then it hit me. I'm trying to think of something clever... Clever, is a dangerous thing. Sometimes from a readability standpoint the most simple solution (as you described and what K.Wolfe tried to implement) is usually the best solution.

    To take K.Wolfe's code and make it workable
    PHP Code:
    foreach ($rules as $key => $rule) {
      if (
    $rule['id'] === 40) {
        unset(
    $rules[$key]);
      }


  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Sometimes from a readability standpoint the most simple solution (as you described and what K.Wolfe tried to implement) is usually the best solution.
    I agree.

    Here's another way to go but you're creating a function. I do like how the one line says it all.

    PHP Code:
    function id_not_40$v ) {
      return 
    $v['id'] != 40;
    }

    $new_array array_filter$orig_array"id_not_40" ); 
    Or all in one but less readable in my opinion.
    PHP Code:
    $new_array array_filter$orig_array, function( $v ) { return $v['id'] != 40; } ); 
    and you may want to call array_values() on it after to re-index the array if desired.
    - Robert

  5. #5
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by QMonkey View Post
    I agree.

    Here's another way to go but you're creating a function. I do like how the one line says it all.

    PHP Code:
    function id_not_40$v ) {
      return 
    $v['id'] != 40;
    }

    $new_array array_filter$orig_array"id_not_40" ); 
    Or all in one but less readable in my opinion.
    PHP Code:
    $new_array array_filter$orig_array, function( $v ) { return $v['id'] != 40; } ); 
    and you may want to call array_values() on it after to re-index the array if desired.

    If you going this route, then you may as well finish it off...

    PHP Code:
    function id_not_in$v$filter ) {
      return 
    $v['id'] != $filter;
    }

    $new_array array_filter$orig_array"id_not_in" ); 

  6. #6
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, now we're getting to be complex again. But for the sake of exercise...

    PHP Code:
    /**
     * Filters a multi-dimentional array based on matching (or not)
     * a key/value pair of the items
     *
     * @param array $data
     * @param string $k
     * @param string $v
     * @param boolean $match
     *
     * @return array
     */
    function special_array_filter$data$k$v$match true ) {
      
    $filter = function ( $item ) use ( $k$v$match ) {
        return ( isset( 
    $item$k ] ) && $item$k ] == $v ) ? $match : !$match;
      };
      
      return 
    array_filter$data$filter );
    }


    // usage:

    $orig_array = array(
      array (
        
    "id" => 11
        
    "rule"=> "rule 1" 
      
    ),
      
      array (
        
    "id" => 40
        
    "rule"=> "rule 2" 
      
    ),
      
      array (
        
    "id" => 55
        
    "rule"=> "rule 3" 
      
    ),
      
      array (
        
    "id" => 40
        
    "rule"=> "rule 4" 
      
    ) );

    $new_array special_array_filter$orig_array'id'40false ); 
    Don't try this at home - unless you need to do something like that in more than one place. But really, just go with the foreach thing, though I do like my first array_filter solution myself.
    - Robert

  7. #7
    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)
    Code:
    <?php
    class Filter extends FilterIterator
    {
        public function accept()
        {
          $current = $this->current();
          return 40 != $current['id'];
        }
    }
    
    
    $array = array(
        array('id' => 12, 'name' => 'foo'),
        array('id' => 40, 'name' => 'foo'),
    );
    
    
    foreach(new Filter(new ArrayIterator($array)) as $item)
    {
        echo $item['id'], ' - ', $item['name'], PHP_EOL;
    }
    
    
    /*
        12 - foo
    */
    Or...

    Code:
    <?php
    class Filter extends FilterIterator
    {
        public function accept()
        {
          $current = $this->current();
          return 40 != $current['id'];
        }
    }
    
    
    $array = array(
        array('id' => 12, 'name' => 'foo'),
        array('id' => 40, 'name' => 'foo'),
    );
    
    
    $array = iterator_to_array(new Filter(new ArrayIterator($array)));
    Again, not quite the easiest way to do this; but I like it.
    @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.

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,121
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by QMonkey View Post
    Here's another way to go but you're creating a function. I do like how the one line says it all.

    PHP Code:
    function id_not_40$v ) {
      return 
    $v['id'] != 40;
    }

    $new_array array_filter$orig_array"id_not_40" ); 
    Or all in one but less readable in my opinion.
    PHP Code:
    $new_array array_filter$orig_array, function( $v ) { return $v['id'] != 40; } ); 
    and you may want to call array_values() on it after to re-index the array if desired.
    array_filter! I knew there was a function that took a callback but earlier for the life of me I couldn't find it! I don't get to use it a lot so I constantly forget about it.

    Quote Originally Posted by AnthonySterling View Post
    Code:
    <?php
    class Filter extends FilterIterator
    {
        public function accept()
        {
          $current = $this->current();
          return 40 != $current['id'];
        }
    }
    
    
    $array = array(
        array('id' => 12, 'name' => 'foo'),
        array('id' => 40, 'name' => 'foo'),
    );
    
    
    foreach(new Filter(new ArrayIterator($array)) as $item)
    {
        echo $item['id'], ' - ', $item['name'], PHP_EOL;
    }
    
    
    /*
        12 - foo
    */
    Or...

    Code:
    <?php
    class Filter extends FilterIterator
    {
        public function accept()
        {
          $current = $this->current();
          return 40 != $current['id'];
        }
    }
    
    
    $array = array(
        array('id' => 12, 'name' => 'foo'),
        array('id' => 40, 'name' => 'foo'),
    );
    
    
    $array = iterator_to_array(new Filter(new ArrayIterator($array)));
    Again, not quite the easiest way to do this; but I like it.
    Now that is an interesting solution. Might fall into the "trying to be too clever" paradigm, but I could see uses for it in a much more complicated scenario.


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
  •