1

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||[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;
    }
   }
  }
7
  • A code should be readable, in the first place, if it must be fixed. Please click on "edit" and indent/format your code. Commented Jun 23, 2012 at 17:55
  • @Thrustmaster : i have formatted code Commented Jun 23, 2012 at 18:00
  • It also needs to be indented properly. Anyway I did that for you.. Commented Jun 23, 2012 at 18:03
  • Your foreach statement is using the variable $vals, should that be $values? It says $finalvalue = array_remove_empty(), do you mean function array_empty()? Commented Jun 23, 2012 at 18:14
  • @codewaggle : sorry i have edit the code Commented Jun 23, 2012 at 18:16

2 Answers 2

3

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

if ($ary_count == 1) return 1; $ary_count is unknown , its may be 2 or 5 or .. some time value can be 2 in first array and even second array can be the same values of first value
how to get the max count of array for example : in foreach i can compare with $ary_count ($maxcount = 8) if($ary_count < $maxcount)
sounds like you want to use a for or while statement. Look here to see which one would best suit your needs: php.net/manual/en/language.control-structures.php
@thrustmaster im trying to check all the array count if first count is less then second count i`m trying to remove ,its not working can you please help unset($prevKey); $count = array(); foreach (array_keys($array) as $key) { $count[$key] = count($array[$key]); if (isset($prevKey) && $count[$prevKey] !== $count[$key]) { unset($array[$prevKey]); } $prevKey = $key; }
Please update your original question with that if it is related, or even better post a new question :)
|
1

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] );
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.