I have an PHP array that looks something like this:
我有一个PHP数组,它看起来像这样
Index Key Value
[0] 1 Awaiting for Confirmation
[1] 2 Assigned
[2] 3 In Progress
[3] 4 Completed
[4] 5 Mark As Spam
When I var_dump the array values i get this:
当我var_dump数组值时,我得到:
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) "Assigned" } [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" } }
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?
我想删除“已完成的”和“标记为垃圾邮件”,我知道我可以取消设置[$array[3],$array[4])将会达到这个目的。但是问题是有时候索引号可能不同,因此是否有办法通过匹配值名来删除“已完成”和“标记为垃圾邮件”?
8 个解决方案
#1
125
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...
这将允许您使用您的键值作为索引来访问数组……
And you'd be able to use functions to search on the values, such as array_search()
:
可以使用函数搜索值,比如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 ?
即使可行,也不是那么简单……我坚持认为:你能不能改变数组的格式,使用更简单的键值系统?
#2
80
...
$array = array(
1 => 'Awaiting for Confirmation',
2 => 'Asssigned',
3 => 'In Progress',
4 => 'Completed',
5 => 'Mark As Spam',
);
return array_values($array);
...
#3
13
$key = array_search("Mark As Spam", $array);
unset($array[$key]);
For 2D arrays...
二维数组…
$remove = array("Mark As Spam", "Completed");
foreach($arrays as $array){
foreach($array as $key => $value){
if(in_array($value, $remove)) unset($array[$key]);
}
}
#4
2
Try this:
试试这个:
$keys = array_keys($array, "Completed");
/edit As mentioned by JohnP, this method only works for non-nested arrays.
/正如JohnP所提到的编辑,此方法仅适用于非嵌套数组。
#5
2
Why do not use array_diff?
为什么不使用array_diff?
$array = array(
1 => 'Awaiting for Confirmation',
2 => 'Asssigned',
3 => 'In Progress',
4 => 'Completed',
5 => 'Mark As Spam',
);
$to_delete = array('Completed', 'Mark As Spam');
$array = array_diff($array, $to_delete);
Just note that your array would be reindexed.
请注意,您的数组将是驯鹿的。
#6
1
You can use this
你可以使用这个
unset($dataArray['key']);
#7
0
The way to do this to take your nested target array and copy it in single step to a non-nested array. Delete the key(s) and then assign the final trimmed array to the nested node of the earlier array. Here is a code to make it simple:
这样做的方法是将嵌套的目标数组一步复制到非嵌套数组中。删除密钥,然后将最后的修剪数组分配给前面数组的嵌套节点。这里有一个简单的代码:
$temp_array = $list['resultset'][0];
unset($temp_array['badkey1']);
unset($temp_array['badkey2']);
$list['resultset'][0] = $temp_array;
#8
0
for single array Item use reset($item)
对于单个数组项使用重置($ Item)
#1
125
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...
这将允许您使用您的键值作为索引来访问数组……
And you'd be able to use functions to search on the values, such as array_search()
:
可以使用函数搜索值,比如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 ?
即使可行,也不是那么简单……我坚持认为:你能不能改变数组的格式,使用更简单的键值系统?
#2
80
...
$array = array(
1 => 'Awaiting for Confirmation',
2 => 'Asssigned',
3 => 'In Progress',
4 => 'Completed',
5 => 'Mark As Spam',
);
return array_values($array);
...
#3
13
$key = array_search("Mark As Spam", $array);
unset($array[$key]);
For 2D arrays...
二维数组…
$remove = array("Mark As Spam", "Completed");
foreach($arrays as $array){
foreach($array as $key => $value){
if(in_array($value, $remove)) unset($array[$key]);
}
}
#4
2
Try this:
试试这个:
$keys = array_keys($array, "Completed");
/edit As mentioned by JohnP, this method only works for non-nested arrays.
/正如JohnP所提到的编辑,此方法仅适用于非嵌套数组。
#5
2
Why do not use array_diff?
为什么不使用array_diff?
$array = array(
1 => 'Awaiting for Confirmation',
2 => 'Asssigned',
3 => 'In Progress',
4 => 'Completed',
5 => 'Mark As Spam',
);
$to_delete = array('Completed', 'Mark As Spam');
$array = array_diff($array, $to_delete);
Just note that your array would be reindexed.
请注意,您的数组将是驯鹿的。
#6
1
You can use this
你可以使用这个
unset($dataArray['key']);
#7
0
The way to do this to take your nested target array and copy it in single step to a non-nested array. Delete the key(s) and then assign the final trimmed array to the nested node of the earlier array. Here is a code to make it simple:
这样做的方法是将嵌套的目标数组一步复制到非嵌套数组中。删除密钥,然后将最后的修剪数组分配给前面数组的嵌套节点。这里有一个简单的代码:
$temp_array = $list['resultset'][0];
unset($temp_array['badkey1']);
unset($temp_array['badkey2']);
$list['resultset'][0] = $temp_array;
#8
0
for single array Item use reset($item)
对于单个数组项使用重置($ Item)