开发者

Deleting from a multi dimensional array

I need to delete from a multi dimensional array.

My array looks开发者_如何转开发 as follows

Array(
   0 => Array(
      0 => "My Album",
      1 => "Testphoto2011-222231.jpg"
   ),
   1 => Array(
      0 => "Test Album",
      1 => "12345.jpg"
   )
);

What I want to do is search the value "My Album" and then delete the entire array from the array.

So for example the values "My Album" & "Testphoto2011-222231.jpg" belong to array[0]. When found I want to delete array[0].

Can anyone help me on this?


<?php
$ar = Array(
   Array(
      "My Album",
      "Testphoto2011-222231.jpg"
   ),
   Array(
      "Test Album",
      "12345.jpg"
   )
);

// Not using foreach, or ascending counting, because
// element removal will screw that up.
for ($i = count($ar) - 1; $i >= 0; $i--) {
   if ($ar[$i][0] == "My Album")
      unset($ar[$i]);
}

$ar = array_values($ar); // re-index

var_export($ar);

/* Output:
array (
  0 => 
  array (
    0 => 'Test Album',
    1 => '12345.jpg',
  ),
)
*/
?>

Live demo.


unset($array[0]) will remove that entry from the array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜