duanhu7615 2012-06-23 17:41
浏览 54
已采纳

检查空数组并删除空数组

i have 100 values in array ,need to check first ten values are not empty and all elements should be same count if not unset the values ,i using "|" for combing all the values ,

I have all the values implode with "|" below is the function which im using im not getting final results as required ,finalvalues is given below ,can you please help fixing this issue

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;
    }
   }
  }
  • 写回答

2条回答 默认 最新

  • dongshan7708 2012-06-23 20:55
    关注

    Here are some much simpler, not crazy ways

    PHP 5.3+

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

    PHP < 5.3

    This edits the $values array directly

    foreach( $values as $k => $value ) {
        if ( preg_replace( '~\s*\|\s*~', '', $value ) == '' || count( explode( '|', $value ) ) !== 8 ) {
            unset( $values[$k] );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向