1

I am trying to unset an array by value. I only have the ExerciseID and need to unset the array that it belongs too.

My array is structured like so:

Array
(
[0] => Array
    (
        [ExerciseID] => 644
        [Sets] => 
        [Reps] => 
    )

[1] => Array
    (
        [ExerciseID] => 33
        [Sets] => 
        [Reps] => 
    )
)

Many thanks in advance.

Taylorsuk
  • 1,419
  • 1
  • 16
  • 51
  • possible duplicate of [Delete element from multidimensional-array based on value](http://stackoverflow.com/questions/4466159/delete-element-from-multidimensional-array-based-on-value) – Elavarasan Apr 07 '14 at 17:23

1 Answers1

1

Loop through the array and check for the ExerciseID key in your array with the value of your ExerciseID and if found , unset and break up from the loop.

$exid=33;
foreach($arr as $k=>$arr1)
{
    if($arr[$k]['ExerciseID']==$exid)
    {
        unset($arr[$k]);
        break;
    }
}

print_r($arr);

OUTPUT :

Array
(
    [0] => Array
        (
            [ExerciseID] => 644
            [Sets] => 
            [Reps] => 
        )

)

Demo

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126