remove elements from array equal to

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

remove elements from array equal to

Post by pedroz »

Code: Select all

[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?

Code: Select all

[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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: remove elements from array equal to

Post by Christopher »

pedroz wrote:Do I need to do a foreach cycle / array check / unset?
Yes, though you could use array_walk().
pedroz wrote:Or is there any php function for it?
Not that I know if. array_slice() would be the closest.
(#10850)
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: remove elements from array equal to

Post by Grizzzzzzzzzz »

Use a foreach loop, unset() the array items that match the id you don't want.

Afterwards you'll either need to sort() the array or use array_values() to re-index it.
Post Reply