【问题标题】:check for empty array and remove empty array检查空数组并删除空数组
【发布时间】:2012-06-23 17:41:26
【问题描述】:

我在数组中有 100 个值,需要检查前十个值是否为空,如果没有取消设置值,所有元素应该是相同的计数,我使用“|”用于梳理所有值,

我的所有值都用“|”内爆下面是我使用的函数,我没有按要求得到最终结果,下面给出了最终值,请你帮忙解决这个问题

finalvalues =array(
    "3" =>"Education|Category|Roles|Industry|Address|Email|Phone|Mobile",
    "4" => "Bsc|computer|SE|Computers||test@test.com|123123132|123234234234"
);

$values = array(
        "0"=> "Computer Student History",
        "1"=> "Computer Student History",
        "2"=> "Computer Student History|batch number",
        "3" =>  "| | | | | | | | | | | | | | | | ",
        "4" => "Education|Category|Roles|Industry|Address|Email|Phone|Mobile",
        "5" => "Bsc|computer|SE|Computers||test@test.com|123123132|123234234234"
);



$newVal = array();
    foreach ($values as $key => $val) { //$values it is..
     $prevalues = explode('|', $val);
     $finalvalue  = array_empty($prevalues ,$full_null=true);
      if($finalvalue == 1){
         unset($prevalues); //why??
     }else{
    $vales = implode('|', $prevalues);
    $newVal[$key] = $vales; //use $key, to preserve the keys here..
     }
     }
   print_r($newVal); //output

  function array_empty($ary, $full_null=false){

   unset($prevKey);
   $count = array();
   $null_count = 0;
   $ary_count = count($ary);
   if ($ary_count == 1) //this means there was no '|', hence no split.
    return 1;

   foreach($array_keys($ary) as $value){
    //      echo $value;
//trying check if first value is less then second value unset array similar second is less then third value unset second .. so the all the array values is same count

$count[$value] = count($ary[$value]);
    if (isset($prevKey) && $count[$prevKey] !== $count[$value]) {
    //unset($array[$prevKey]);
    return 1;
    }

    if($value == NULL || trim($value) == "" ){ // trim(..) was what you wanted.
        $null_count++;
    }
   }

if($full_null == true){
    if($null_count == $ary_count){
        return  1;
    }else{
        return 0;
    }
   }
  }

【问题讨论】:

  • 代码应该是可读的,如果它必须被修复的话。请点击“编辑”并缩进/格式化您的代码。
  • @Thrustmaster : 我已经格式化了代码
  • 它也需要正确缩进。无论如何,我为你做到了..
  • 您的 foreach 语句正在使用变量 $vals,应该是 $values 吗?上面写着$finalvalue = array_remove_empty(),你的意思是function array_empty()吗?
  • @codewaggle : 抱歉我已经编辑了代码

标签: php arrays


【解决方案1】:

这应该可以帮助您(使用内联 cmets):

$newVal = array();
foreach ($values as $key => $val) { //$values it is..
    $prevalues = explode('|', $val);
    $finalvalue  = array_empty($prevalues ,$full_null=true);
    if($finalvalue == 1){
        unset($prevalues); //why??
    }else{
        $vales = implode('|', $prevalues);
        $newVal[$key] = $vales; //use $key, to preserve the keys here..
    }
}
print_r($newVal); //output

function array_empty($ary, $full_null=false){
    $null_count = 0;
    $ary_count = count($ary);
    if ($ary_count == 1) //this means there was no '|', hence no split.
        return 1;

    foreach($ary as $value){
    //      echo $value;
        if($value == NULL || trim($value) == "" ){ // trim(..) was what you wanted.
            $null_count++;
        }
    }

    if($full_null == true){
        if($null_count == $ary_count){
            return  1;
        }else{
            return 0;
        }
    }
}

【讨论】:

  • 如果 ($ary_count == 1) 返回 1; $ary_count 未知,它可能是 2 或 5 或 .. 第一个数组中的某个时间值可以是 2,甚至第二个数组也可以是第一个值的相同值
  • 如何获取数组的最大计数,例如:在 foreach 中我可以与 $ary_count ($maxcount = 8) if($ary_count 进行比较
  • 听起来你想使用 for 或 while 语句。在这里查看哪一个最适合您的需求:php.net/manual/en/language.control-structures.php
  • @thrustmaster 我正在尝试检查所有数组计数,如果第一个计数小于我尝试删除的第二个计数,它不起作用,请您帮忙取消设置($prevKey); $count = 数组(); foreach (array_keys($array) as $key) { $count[$key] = count($array[$key]); if (isset($prevKey) && $count[$prevKey] !== $count[$key]) { unset($array[$prevKey]); } $prevKey = $key; }
  • 请更新您的原始问题,如果它是相关的,或者更好post a new question :)
【解决方案2】:

这里有一些更简单,而不是疯狂的方法

PHP 5.3+

$final = array_filter(
    $values,
    function( $v ){
        return
            preg_replace( '~\s*\|\s*~', '', $v ) &&
            count( explode( '|', $v ) ) === 8;
    }
);

PHP

这会直接编辑 $values 数组

foreach( $values as $k => $value ) {
    if ( preg_replace( '~\s*\|\s*~', '', $value ) == '' || count( explode( '|', $value ) ) !== 8 ) {
        unset( $values[$k] );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2020-06-03
    • 2023-03-09
    相关资源
    最近更新 更多