从关联数组PHP删除键 [英] PHP Remove key from associative array

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

问题描述

我有一个PHP数组,看起来是这样的:

I have an PHP array that looks something like this:

Index              Key        Value
[0]                1       Awaiting for Confirmation
[1]                2       Asssigned
[2]                3       In Progress
[3]                4       Completed
[4]                5       Mark As Spam

在我的var_dump数组值我得到这样的:

When I var_dump the array values i get this:

array(5) { [0]=> array(2) { ["key"]=> string(1) "1" ["value"]=> string(25) "Awaiting for Confirmation" } [1]=> array(2) { ["key"]=> string(1) "2" ["value"]=> string(9) "Asssigned" } [2]=> array(2) { ["key"]=> string(1) "3" ["value"]=> string(11) "In Progress" } [3]=> array(2) { ["key"]=> string(1) "4" ["value"]=> string(9) "Completed" } [4]=> array(2) { ["key"]=> string(1) "5" ["value"]=> string(12) "Mark As Spam" } } 

我想删除已完成和标记为垃圾邮件我知道我可以不设置[$阵列[3],$阵列[4])会做的伎俩。但问题是,有时索引号可以不同,因此是有办法通过匹配值的名称去掉已完成和标记为垃圾邮件?

I wanted to remove "Completed" and "Mark As Spam" I know I can unset[$array[3],$array[4]) will do the trick. But the problem is that sometime the index number can be different therefore is there a way to remove "completed" and "Mark As Spam" by matching the value name?

感谢这么多提前

推荐答案

您阵列是很奇怪:为什么不直接使用为指标,而为...价值?

Your array is quite strange : why not just use the key as index, and the value as... the value ?

那岂不是一个容易得多,如果你的阵列声明如下:

Wouldn't it be a lot easier if your array was declared like this :

$array = array(
    1 => 'Awaiting for Confirmation', 
    2 => 'Asssigned', 
    3 => 'In Progress', 
    4 => 'Completed', 
    5 => 'Mark As Spam', 
);

这将允许您使用您的键的值作为索引访问数组...

That would allow you to use your values of key as indexes to access the array...

和你能够使用功能上的值进行搜索,比如 为array_search()

And you'd be able to use functions to search on the values, such as array_search() :

$indexCompleted = array_search('Completed', $array);
unset($array[$indexCompleted]);

$indexSpam = array_search('Mark As Spam', $array);
unset($array[$indexSpam]);

var_dump($array);

更容易比你的数组,不是吗?

Easier than with your array, no ?

结果

相反,你的阵列看起来像这样的:

Instead, with your array that looks like this :

$array = array(
    array('key' => 1, 'value' => 'Awaiting for Confirmation'), 
    array('key' => 2, 'value' => 'Asssigned'), 
    array('key' => 3, 'value' => 'In Progress'), 
    array('key' => 4, 'value' => 'Completed'), 
    array('key' => 5, 'value' => 'Mark As Spam'), 
);

您将不得不遍历所有条目,来分析,并取消设置正确的项目:

You'll have to loop over all items, to analyse the value, and unset the right items :

foreach ($array as $index => $data) {
    if ($data['value'] == 'Completed' || $data['value'] == 'Mark As Spam') {
        unset($array[$index]);
    }
}
var_dump($array);

即使这样做,能,它不是那么简单......我坚持:你能不能改变你的阵列的形式,用一个简单的键/值系统正常工作

Even if do-able, it's not that simple... and I insist : can you not change the format of your array, to work with a simpler key/value system ?

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

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