-1

我有一个包含其他数组(子数组)的数组。我需要获取包含特定键值的子数组的索引。例如。这是我的数组:

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] 中删除。我知道 unset($myarray[2]) 的一种方法,但我怎样才能得到这个索引 [2]。如果我只知道 [ani] 键值 472367427。如何在 var 中获取此“[2]”以将其插入 unset 命令。如果我需要删除具有 = 1345436 的键 [ani] 的子数组(它在数组 [1] 中)。有没有办法通过键的值来搜索数组的索引。同样,如何在多维数组中通过 [ani] 键找到这个索引 [2] 或 [1]?谢谢!

4

5 回答 5

2

我认为这应该有效(未经测试 - 但你明白了)

foreach ($arrays as $key => $item)
{
    if ($item['ani'] === '472367427')
    {
        unset($arrays[$key]);
    }
}
于 2013-07-09T12:55:19.157 回答
2

有几种方法可以解决这个问题,但array_filter可能是最具可扩展性的。您将需要创建一个回调函数来搜索要删除的值,然后将其用作数组的过滤器:

function filterCallback($value) {
   if($value['ani'] == "472367427") {
      return false;
    } else {
      return true;
    }
}

$array = array_filter($array,'filterCallback');

这样做的好处是您可以抽象过滤逻辑(并使其更复杂),而不必在foreach循环中进行。

http://www.php.net/manual/en/function.array-filter.php

于 2013-07-09T12:58:52.257 回答
1
$remove = 472367427;

foreach($your_array as $key => $values) {
    if(!empty($values['ani'] && $values['ani'] == $remove) {
        unset($your_array[$key]);
    }
}
于 2013-07-09T12:55:29.650 回答
0

如果您已经知道要查找的 ani 值:

$yourani = 'Your known ani value';

foreach($myarray AS $array){
   if($array['ani'] == $yourani){
      unset($array);
   }
}
于 2013-07-09T12:56:07.967 回答
0

您可以像这样使用array_search获取索引和unset元素

$a = array('one', 'two', 'three', 472367427, 'four', 'five');
unset($a[array_search(472367427, $a)]);
于 2013-07-09T13:01:52.867 回答