在fo​​reach循环解封数组值 [英] Unsetting array values in a foreach loop

查看:159
本文介绍了在fo​​reach循环解封数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个foreach循环设置要经过我的数组,检查是否有一定的联系,如果发现它会从数组链接。

我的code:

 的foreach($图像,图像$)
{
    如果($形象=='http://i27.tinypic.com/29yk345.gif'||
    $图像=='http://img3.abload.de/img/10nx2340fhco.gif'||
    $图像=='http://i42.tinypic.com/9pp2456x.gif')
    {
        未设置($图像[$图片]);
    }
}

但它不会删除阵列entires。这可能是与 $图像[$图片] ,因为这不是数组项,只有内容的钥匙吗?有没有办法做到这一点不纳入反?

感谢。

编辑:感谢球员,但现在我有一个问题,其中数组项实际上没有被删除

我的新code:

 的foreach($图像[1] $关键=> $图)
{
    如果($形象=='http://i27.tinypic.com/29yk345.gif')
    $图像=='http://img3.abload.de/img/10nx2340fhco.gif'||
    $图像=='http://i42.tinypic.com/9pp2456x.gif')
    {
        未设置($图像[$关键]);
    }
}

$图像是一个actuallty二维数组所以现在我为什么需要$图像[1]。我已经确认并成功地绕着数组元素,有些元素也确实有一些,我想删除这些网址,但他们没有被清除。这是我的 $图像数组:

 阵列

    [0] =>排列
        (
            [0] =>无用
            [1] =>无用
            [2] =>无用
            [3] =>无用
            [4] =>无用
        )    [1] =>排列
        (
            [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
        ))

谢谢!


解决方案

 的foreach($图像,$关键=> $图)
{
    如果(in_array($形象,阵列(
       http://i27.tinypic.com/29ykt1f.gif',
       http://img3.abload.de/img/10nxjl0fhco.gif',
       http://i42.tinypic.com/9pp2tx.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!

解决方案

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]);
    }
}

这篇关于在fo​​reach循环解封数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆