取消 foreach 循环中的数组值

我设置了一个 foreach 循环来遍历我的数组,检查某个链接,如果它发现它从数组中删除了该链接。

我的代码:

foreach($images as $image)
{
if($image == 'http://i27.tinypic.com/29yk345.gif' ||
$image == 'http://img3.abload.de/img/10nx2340fhco.gif' ||
$image == 'http://i42.tinypic.com/9pp2456x.gif')
{
unset($images[$image]);
}
}

但它不会删除整个数组。它可能与 $images[$image]有关,因为它不是数组条目的键,而只是内容?有没有办法不用计数器就能做到这一点?

谢谢。

编辑: 谢谢大家,但是现在我遇到了另一个问题,数组条目实际上并没有被删除。

我的新代码:

foreach($images[1] as $key => $image)
{
if($image == 'http://i27.tinypic.com/29yk345.gif')
$image == 'http://img3.abload.de/img/10nx2340fhco.gif' ||
$image == 'http://i42.tinypic.com/9pp2456x.gif')
{
unset($images[$key]);
}
}

$images 实际上是一个二维数组,因此我需要 $images [1]。我已经检查过了,它成功地绕过了数组元素,有些元素确实有一些我想删除的 URL,但是它们没有被删除。这是我的 $images数组:

Array
(
[0] => Array
(
[0] => useless
[1] => useless
[2] => useless
[3] => useless
[4] => useless
)


[1] => Array
(
[0] => http://i27.tinypic.com/29yk345.gif
[1] => http://img3.abload.de/img/10nx2340fhco.gif
[2] => http://img3.abload.de/img/10nx2340fhco.gif
[3] => http://i42.tinypic.com/9pp2456x.gif
)


)

谢谢!

148020 次浏览
foreach($images as $key => $image)
{
if(in_array($image, array(
'http://i27.tinypic.com/29ykt1f.gif',
'http://img3.abload.de/img/10nxjl0fhco.gif',
'http://i42.tinypic.com/9pp2tx.gif',
))
{
unset($images[$key]);
}
}

$image is in your case the value of the item and not the key. Use the following syntax to get the key too:

foreach ($images as $key => $value) {
/* … */
}

Now you can delete the item with unset($images[$key]).


foreach($images as $key=>$image)
{
if($image == 'http://i27.tinypic.com/29ykt1f.gif' ||
$image == 'http://img3.abload.de/img/10nxjl0fhco.gif' ||
$image == 'http://i42.tinypic.com/9pp2tx.gif')
{ unset($images[$key]); }
}

!!foreach($images as $key=>$image

cause $image is the value, so $images[$image] make no sense.

Try that:

foreach ($images[1] as $key => &$image) {
if (yourConditionGoesHere) {
unset($images[1][$key])
}
}
unset($image); // detach reference after loop

Normally, foreach operates on a copy of your array so any changes you make, are made to that copy and don't affect the actual array.

So you need to unset the values via $images[$key];

The reference on &$image prevents the loop from creating a copy of the array which would waste memory.

One solution would be to use the key of your items to remove them -- you can both the keys and the values, when looping using foreach.

For instance :

$arr = array(
'a' => 123,
'b' => 456,
'c' => 789,
);


foreach ($arr as $key => $item) {
if ($item == 456) {
unset($arr[$key]);
}
}


var_dump($arr);

Will give you this array, in the end :

array
'a' => int 123
'c' => int 789


Which means that, in your case, something like this should do the trick :

foreach($images as $key => $image)
{
if($image == 'http://i27.tinypic.com/29yk345.gif' ||
$image == 'http://img3.abload.de/img/10nx2340fhco.gif' ||
$image == 'http://i42.tinypic.com/9pp2456x.gif')
{
unset($images[$key]);
}
}

Sorry for the late response, I recently had the same problem with PHP and found out that when working with arrays that do not use $key => $value structure, when using the foreach loop you actual copy the value of the position on the loop variable, in this case $image. Try using this code and it will fix your problem.

for ($i=0; $i < count($images[1]); $i++)
{


if($images[1][$i] == 'http://i27.tinypic.com/29yk345.gif' ||


$images[1][$i] == 'http://img3.abload.de/img/10nx2340fhco.gif' ||


$images[1][$i] == 'http://i42.tinypic.com/9pp2456x.gif')


{


unset($images[1][$i]);


}


}


var_dump($images);die();

You would also need a

$i--;

after each unset to not skip an element/

Because when you unset $item[45], the next element in the for-loop should be $item[45] - which was [46] before unsetting. If you would not do this, you'd always skip an element after unsetting.

You can use the index of the array element to remove it from the array, the next time you use the $list variable, you will see that the array is changed.

Try something like this

foreach($list as $itemIndex => &$item) {


if($item['status'] === false) {
unset($list[$itemIndex]);
}


}

To answer the initial question (after your edit), you need to unset($images[1][$key]);

Now some more infos how PHP works: You can safely unset elements of the array in foreach loop, and it doesn't matter if you have & or not for the array item. See this code:

$a=[1,2,3,4,5];
foreach($a as $key=>$val)
{
if ($key==3) unset($a[$key]);
}
print_r($a);

This prints:

Array
(
[0] => 1
[1] => 2
[2] => 3
[4] => 5
)

So as you can see, if you unset correct thing within the foreach loop, everything works fine.