在 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
)
)
谢谢!
I have a foreach loop set up to go through my array, check for a certain link, and if it finds it removes that link from the array.
My code:
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]);
}
}
But it doesn't remove the array entires. It's probably something to do with $images[$image]
, as that's not the key of the array entry, only the content? Is there a way to do this without incorporating a counter?
Thanks.
EDIT: Thanks guys, but now I have another problem where the array entries don't actually get deleted.
My new code:
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 is actuallty a two-dimensional array now hence why I need $images[1]. I have checked and it successfully goes around the array elements, and some elements do actually have some of those URLs in that I wish to delete, but they're not getting deleted. This is my $images
array:
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
)
)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您还需要
在每次取消设置后不跳过元素/
因为当您取消设置
$item[45]
时,for 循环中的下一个元素应该是$item[45]< /code> - 取消设置之前为
[46]
。如果您不这样做,则在取消设置后您总是会跳过某个元素。You would also need a
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.抱歉回复晚了,我最近在 PHP 上遇到了同样的问题,并发现在处理不使用
$key => 的数组时$value
结构,当使用foreach
循环时,您实际复制循环变量上位置的值,在本例中是$image
。尝试使用此代码,它将解决您的问题。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 theforeach
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.尝试一下:
通常,
foreach
对数组的副本进行操作,因此您所做的任何更改都会对该副本进行,并且不会影响实际的数组。因此,您需要通过
$images[$key]
取消设置值;&$image
上的引用可防止循环创建数组的副本,这会浪费内存。Try that:
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.要回答最初的问题(编辑后),您需要 unset($images[1][$key]);
现在更多关于 PHP 如何工作的信息:
您可以安全地在 foreach 循环中取消设置数组的元素,并且如果您有 & 也没关系。或不用于数组项。请参阅此代码:
此打印:
如您所见,如果您在 foreach 循环中取消设置正确的内容,则一切正常。
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:
This prints:
So as you can see, if you unset correct thing within the foreach loop, everything works fine.
您可以使用数组元素的索引将其从数组中删除,下次使用
$list
变量时,您将看到数组已更改。尝试这样的事情
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
$image
在您的情况下是项目的值而不是键。也可以使用以下语法获取密钥:现在您可以使用
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:Now you can delete the item with
unset($images[$key])
.一种解决方案是使用项目的键来删除它们 - 当使用 foreach.
例如:
最后会给你这个数组:
这意味着,就您而言,类似这样的事情应该可以解决问题:
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 :
Will give you this array, in the end :
Which means that, in your case, something like this should do the trick :
!!foreach($images as $key=>$image
因为 $image 是值,所以 $images[$image] 没有意义。
!!foreach($images as $key=>$image
cause $image is the value, so $images[$image] make no sense.