如果它们的值少于两个,如何删除某些特定的数组键

时间:2016-01-19 11:21:21

标签: php arrays

我有一个带有两个不同索引的多个数组([IP]和[all_dates])

输出如下:

Array
(
    [0] => Array
        (
            [ip] => 72.xx.xxx.xxx
            [all_dates] => Array
                (
                    [0] => [12/Oct/2015:00:30:15
                    [1] => [12/Oct/2015:00:30:24
                    [2] => [12/Oct/2015:00:30:49
                    [3] => [12/Oct/2015:00:30:57
                    [4] => [12/Oct/2015:00:30:57

 [2] => Array
        (
            [ip] => 192.xxx.xxx.xxx
            [all_dates] => Array
                (
                    [0] => [12/Oct/2015:00:33:06
                    [1] => [12/Oct/2015:00:33:06
                    [2] => 
                )

        )

 [3] => Array
        (
            [ip] => 216.xxx.xxx.xxx
            [all_dates] => Array
                (
                    [0] => [12/Oct/2015:00:41:09
                    [1] => 
                )

现在我想删除[all_dates]少于2个值的每个数组键

(就像[3]数组一样)!

首先,我需要删除所有空键,如([2] => (empty)。在此之后,我可以管理所有少于2个值的数组键。

感谢您的帮助!

输出应该在最后看起来像这样:

Array
(
    [0] => Array
        (
            [ip] => 72.xx.xxx.xxx
            [all_dates] => Array
                (
                    [0] => [12/Oct/2015:00:30:15
                    [1] => [12/Oct/2015:00:30:24
                    [2] => [12/Oct/2015:00:30:49
                    [3] => [12/Oct/2015:00:30:57
                    [4] => [12/Oct/2015:00:30:57

 [2] => Array
        (
            [ip] => 192.xxx.xxx.xxx
            [all_dates] => Array
                (
                    [0] => [12/Oct/2015:00:33:06
                    [1] => [12/Oct/2015:00:33:06
                )

        )

这是我如何获得我的多个数组。我真的无法解释为什么PHP总是说我的多个数组($ ip_with_dates)没有定义。

$diff_ips = array_unique($ip_array);
foreach ($diff_ips as $ip) {
    $get_dates = shell_exec("grep $ip $path" . $inputs['domain'] . ".log | awk '{print $4}'");
    $array_date = explode("\n", $get_dates);
    $ip_with_dates[] = [
        'ip' => $ip,
        'all_dates' => $array_date
    ];
}

我尝试解决方案:

foreach ($ip_with_dates['all_dates'] as $dates) {
    array_filter($dates['all_dates']);
    if(count($dates['all_dates']) < 2){
        unset($dates);
    }
}

2 个答案:

答案 0 :(得分:2)

您可以非常简单地使用array_filter()来完成此过程

<?php
$in = array(
    0 => array('ip' => '72.xx.xxx.xxx',
               'all_dates' => array(
                    '0' => '12/Oct/2015:00:30:15',
                    '1' => '12/Oct/2015:00:30:24',
                    '2' => '12/Oct/2015:00:30:49',
                    '3' => '12/Oct/2015:00:30:57',
                    '4' => '12/Oct/2015:00:30:57'
               )
      ),
   1 => array('ip' => '192.xxx.xxx.xxx',
              'all_dates' => array(
                    '0' => '12/Oct/2015:00:33:06',
                    '1' => '12/Oct/2015:00:33:06',
                    '2' => ''
              )
       ),
   2 => array( 'ip' => '216.xxx.xxx.xxx',
               'all_dates' => array(
                    '0' => '12/Oct/2015:00:41:09',
                    '1' => ''
             )
       )
);

$array = array_filter($in, function($element) {
                                return count($element['all_dates']) > 2 ? $element : '';
                           });

print_r($array);

哪会产生这个

Array
(
    [0] => Array
        (
            [ip] => 72.xx.xxx.xxx
            [all_dates] => Array
                (
                    [0] => 12/Oct/2015:00:30:15
                    [1] => 12/Oct/2015:00:30:24
                    [2] => 12/Oct/2015:00:30:49
                    [3] => 12/Oct/2015:00:30:57
                    [4] => 12/Oct/2015:00:30:57
                )

        )

    [1] => Array
        (
            [ip] => 192.xxx.xxx.xxx
            [all_dates] => Array
                (
                    [0] => 12/Oct/2015:00:33:06
                    [1] => 12/Oct/2015:00:33:06
                    [2] =>
                )

        )

)

RE:您更新的问题

而不是在创建数组后试图摆弄数组,为什么不将测试作为原始数组构建过程的一部分进行,如下所示

$diff_ips = array_unique($ip_array);
foreach ($diff_ips as $ip) {
    $get_dates = shell_exec("grep $ip $path" . $inputs['domain'] . ".log | awk '{print $4}'");
    $array_date = explode("\n", $get_dates);

    if ( count($array_dates) > 2 ) {
        $ip_with_dates[] = [
                           'ip' => $ip,
                           'all_dates' => $array_date
                           ];
    }
}

答案 1 :(得分:1)

您可以执行类似

的操作
  1. 使用array_filter()清除空值的all_dates子数组。

  2. 检查子数组元素的总数是< 2,如果是,则取消设置相应的数组。

  3. 假设父数组为$parent

    foreach($parent as &$child){ // passing the elements by reference so un-setting them will affect the actual parent array. 
      if(isset($child['all_dates'])){ // maybe inconsistent data, so check if the "key" all dates is set 
        array_filter($child['all_dates']); // remove all empty elements
        if(count($child['all_dates']) < 2){
          unset $child;
        }
      } else { // if the key "all_dates" not set then discard the child. 
        unset $child;
      }
    }
    

    根据编辑进行更新:

    您需要先定义数组

    $ip_with_dates = array(); // oldschool array declaration
    

    $ip_with_dates = []; // shorthand array declaration as of PHP 5.4
    

    然后继续foreach。 如下图所示..

            $ip_with_dates = array();
            $diff_ips = array_unique($ip_array);
            foreach ($diff_ips as $ip) {
                $get_dates = shell_exec("grep $ip $path" . $inputs['domain'] . ".log | awk '{print $4}'");
                $array_date = explode("\n", $get_dates);
                $ip_with_dates[] = [
                    'ip' => $ip,
                    'all_dates' => $array_date
                ];
             }