Page 1 of 1

remove elements from array equal to

Posted: Mon Sep 24, 2012 12:15 pm
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?

Re: remove elements from array equal to

Posted: Mon Sep 24, 2012 5:07 pm
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.

Re: remove elements from array equal to

Posted: Tue Sep 25, 2012 7:19 am
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.