简体   繁体   English

检查空数组并删除空数组

[英]check for empty array and remove empty array

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 "|" 我在数组中有100个值,需要检查前十个值是否为空,所有元素应该是相同的数,如果没有取消设置值,我使用“|” 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;
    }
   }
  }

This should help you (with inline comments): 这应该对你有所帮助(内联评论):

$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;
        }
    }
}

Here are some much simpler, not crazy ways 这里有一些更简单,更疯狂的方法

PHP 5.3+ PHP 5.3+

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

PHP < 5.3 PHP <5.3

This edits the $values array directly 这直接编辑$ values数组

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM