我有一个包含其他数组(子数组)的数组。我需要得到包含键的特定值的子数组的索引。例如。这是我的阵列:
Array
(
[0] => Array
(
[id] => 353
[name] => account_2
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 526466
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] =>
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[1] => Array
(
[id] => 352
[name] => account_3
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 1345436
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] =>
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[2] => Array
(
[id] => 354
[name] => account_4
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 472367427
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] =>
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
)
我需要什么。例如,我需要从数组子阵列[2]中删除。我知道一种未设置的方法($ myarray [2]),但我怎样才能得到这个索引[2]。如果我只知道[ani]键值472367427.如何在var中将此“[2]”插入到unset命令中。 如果我需要删除具有键[ani] = 1345436(它在数组[1]中)的子数组。有没有办法按键的值搜索数组的索引。 再次,如何在多维数组中通过[ani]键找到这个索引[2]或[1]? 谢谢!
答案 0 :(得分:2)
我认为这应该有用(没有经过测试 - 但你明白了)
foreach ($arrays as $key => $item)
{
if ($item['ani'] === '472367427')
{
unset($arrays[$key]);
}
}
答案 1 :(得分:2)
有几种方法可以解决这个问题,但array_filter
可能是最具扩展性的。您需要创建一个回调函数来搜索您想要删除的值,然后将其用作数组的过滤器:
function filterCallback($value) {
if($value['ani'] == "472367427") {
return false;
} else {
return true;
}
}
$array = array_filter($array,'filterCallback');
这样做的好处是你可以抽象你的过滤逻辑(并使其更复杂),而不必在foreach
循环内完成。
答案 2 :(得分:1)
$remove = 472367427;
foreach($your_array as $key => $values) {
if(!empty($values['ani'] && $values['ani'] == $remove) {
unset($your_array[$key]);
}
}
答案 3 :(得分:1)
如果您已经知道要查找的ani值:
$yourani = 'Your known ani value';
foreach($myarray AS $array){
if($array['ani'] == $yourani){
unset($array);
}
}
答案 4 :(得分:0)
您可以使用array_search
获取索引,使用unset
这样的元素
$a = array('one', 'two', 'three', 472367427, 'four', 'five');
unset($a[array_search(472367427, $a)]);