从PHP数组删除值 [英] Removing a value from a PHP Array

查看:174
本文介绍了从PHP数组删除值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP我想基于元素的值从一个数组中删除元素。

例如用下面的数组:

 阵列

    [671] =>排列
        (
            [0] => 1
            [1] => 100
            [2] => 1000
        )
    [900] =>排列
        (
            [0] => 15
            [1] => 88
        }

我希望能够指定对内部阵列可以删除该值。因为如果我指定的100个实例的结果数组将如下所示:

 阵列

    [671] =>排列
        (
            [0] => 1
            [2] => 1000
        )
    [900] =>排列
        (
            [0] => 15
            [1] => 88
        }

我的第一个想法是通过使用的foreach和取消了违规的价值时,我发现它的阵列循环,但是这似乎并没有引用原始数组,只是创建的循环变量。

感谢。


解决方案

 的foreach($数组的$ id => $数据){
    的foreach($数据,$指数=> $ offending_val){
         如果($ offending_val === 100){
            未设置($数组[$ ID] [$指数]);
         }
    }
}

Using PHP I'm trying to remove an element from an array based on the value of the element.

For example with the following array:

Array
(
    [671] => Array
        (
            [0] => 1
            [1] => 100
            [2] => 1000
        )
    [900] => Array
        (
            [0] => 15
            [1] => 88
        }
)

I'd like to be able to specify a value of on of the inner arrays to remove. For example if I specified 100 the resulting array would look like:

Array
(
    [671] => Array
        (
            [0] => 1
            [2] => 1000
        )
    [900] => Array
        (
            [0] => 15
            [1] => 88
        }
)

My first thought was to loop through the array using foreach and unset the "offending" value when I found it, but that doesn't seem to reference the original array, just the loop variables that were created.

Thanks.

解决方案

foreach($array as $id => $data){
    foreach($data as $index => $offending_val){
         if($offending_val === 100){
            unset($array[$id][$index]);
         }
    }
}

这篇关于从PHP数组删除值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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