2012-06-23 62 views
1

我有100個值在數組中,需要檢查前十個值不爲空,所有元素應該是相同的計數,如果不取消設置值,我使用「|」用於梳理所有的值,檢查空陣列和刪除空陣列

我有所有的價值與「|」下面是即時通訊使用im沒有得到最終的結果需要,finalvalues下面給出的功能,你可以請幫忙解決這個問題

finalvalues =array(
    "3" =>"Education|Category|Roles|Industry|Address|Email|Phone|Mobile", 
    "4" => "Bsc|computer|SE|Computers||[email protected]|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||[email protected]|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; 
    } 
    } 
    } 
+0

A碼應該是可讀的,在第一位置,如果它必須是固定的。請點擊「編輯」並縮進/格式化您的代碼。 – SuperSaiyan

+0

@Thrustmaster:我已格式化代碼 – user1477117

+0

它也需要正確縮進。無論如何,我爲你做的.. – SuperSaiyan

回答

1

這裏有一些簡單得多,沒有瘋狂的方式

PHP 5.3+

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

PHP < 5.3

此編輯$值陣列直接

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

這會幫助你(其內部註釋):

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

if($ ary_count == 1)return 1; $ ary_count是未知的,它可能是2或5或..某些時間值可以是第一個數組中的2,甚至第二個數組可以是第一個值的相同值 – user1477117

+0

我已編輯$ values值 – user1477117

+0

如何獲取最大計數數組例如:在foreach中,我可以與$ ary_count($ maxcount = 8)進行比較,如果($ ary_count <$ maxcount) – user1477117