过滤两个php数组

时间:2017-08-14 20:12:29

标签: php arrays conditional

我有两个数组。我已将两个数组合并输出总数

1st array =

$farray = Array ( 
    [0] => Array ( 
        [1] => Array ( 
                [ED] => 15 
                [EN] => 14
                )
         )
    [1] => Array ( 
        [2] => Array ( 
                [ED] => 5 
                [EN] => 10 
               )
          )
     )

2nd Array =

$tarray = Array ( 
    [0] => Array ( 
        [1] => Array ( 
                [ED] => 45 
                [EN] => 50                  
            ) 
        ) 
    [1] => Array ( 
        [2] => Array ( 
                [ED] => 38 
                [EN] => 40
               )
         )
    )

以上两个数组的组合:

  $all = Array ( 
       [0] => Array ( 
            [1] => Array ( 
                [ED] => 60
                [EN] => 64                  
               ) 
            ) 
       [1] => Array ( 
            [2] => Array ( 
                 [ED] => 43 
                 [EN] => 50
                  ) 
             )
        )

现在我想使用第一个数组和第二个数组将条件插入以下代码:

$fscore = array_reduce(
$farray, 
function($farray, $item) {
    $id = key($item);
    $scores = $item[$id];
    $farray[$id] = array(
    "score" => array_sum($scores),
    "farray"=> min($scores)>=7      
   );
    return $farray;
   },
   array()
 );

上述尝试有效并输出以下内容(print_r($fscore)):

 Array ( 
        [1] => Array ( 
            [score] => 124
            [farray] => 1 
            ) 
        [2] => Array ( 
            [score] => 93
            [farray] => 0
            ) 
      )

但我想为它添加更多条件并与下面的$tarray结合使用:

 $all= array_reduce(
 $all, 
  function($all, $item) {
    $id = key($item);
    $scores = $item[$id];
    $all[$id] = array(
    "score" => array_sum($scores),
    "farray"=> min($scores)>=7
    //if tarray key==ED 23 else 26 for minimum `$scores`
    "tarray"=> min($scores)>='ED'? 23:26//ternary operator
   );
    return $all;
   },
   array()
 );

我不知道如何插入$ tarray。如前所述,没有$ tarray,它可以工作。

我的尝试失败,但未输出预期结果。在这个问题中,我只使用ED和EN(记分键)来节省更多空间。 $ farray预期的最低分数大于或等于7,而$ tarray预期的最低分数为23/26。如果得分键是$ tarray中的ED,则最小分数大于或等于23,否则它必须为26.根据这些条件,我想返回true或false值。请帮忙。以下是我的尝试:

    $farray = array(
            array (
                1=> array(
                    "ED"=>15,
                    "EN"=>14
                   )
               ),
            array(
               2=>array(
                    "ED"=>5,
                    "EN"=>10
                   )
                 )
               );

$tarray = array ( 
         array ( 
           1 => array ( 
                "ED" => 45, 
                "EN" => 50                  
            ) 
        ), 
         array ( 
          2 => array ( 
                "ED" => 38, 
                "EN" => 40
               )
         )
    );

$combine = array ( 
       array ( 
            1 => array ( 
                "ED" => 60,
                "EN" => 64                  
               ) 
            ), 
      array ( 
            2 => array ( 
                 "ED" => 43 ,
                 "EN" => 50
                  ) 
             )
        );

function filtertArray($value){
    foreach($value as $key =>$val){
    foreach($val as $k=>$v){
        foreach($v as $t=>$m){
            if($t=='ED'){
               return $m>=23;
            }else{
               return $m>=26;
            }            
        }
    }
  }
}
function filterfArray($value){
    foreach($value as $key =>$val){
    foreach($val as $k=>$v){
        foreach($v as $t=>$m){            
           return $m>=7;                   
        }
    }
  }
}

 $all = array_reduce(
     $combine, 
     function($combine, $item) use ($farray, $tarray) {
        $id = key($item);
        $scores = $item[$id];
        $combine[$id] = array(
              "score" => array_sum($scores),
              "farray"=> array_filter($farray,"filterfArray"),
              "tarray"=> array_filter($tarray,"filtertArray")
            );
         return $combine;
     },
     array()
   );
echo "<pre>";
print_r($all);
echo "</pre>";

输出:

E_WARNING : type 2 -- Invalid argument supplied for foreach()

此代码的预期输出为:

Array ( 
    [1] => Array ( 
        [score] => 124
        [farray] => true//1
        [tarray] => true//1
        ) 
    [2] => Array ( 
        [score] => 93
        [farray] => false//0
        [tarray] => true//1
        ) 
  )

1 个答案:

答案 0 :(得分:0)

我认为我不打算通过array_reduce()电话来解决这个问题。更容易理解地提供正确的比较和结果。您当然欢迎将我的foreach循环系列转换为数组函数。

我的最新编辑使用新建议的密钥添加了更多样本分数数据。对$fscore的{​​{1}}比较的调整是,现在将最低的非tarray分数与ED进行比较。

代码:(Demo

26

输出:

$farray=[[1=>['ED'=>15,'EN'=>14,'MT'=>16,'MZ'=>20]],[2=>['ED'=>5,'EN'=>10,'MT'=>36,'MZ'=>30]]];
$tarray=[[1=>['ED'=>45,'EN'=>50,'MT'=>28,'MZ'=>27]],[2=>['ED'=>38,'EN'=>40,'MT'=>56,'MZ'=>60]]];

// generate $all
foreach($farray as $i=>$a){
    foreach($a as $n=>$scores){
        foreach($scores as $k=>$v){
            $all[$i][$n][$k]=$v+$tarray[$i][$n][$k];  // sum the relative scores
        }
    }
}
var_export($all);
echo "\n\n";

// generate $fscores
$sub23_keys=array_flip(['ED']);  // store list of keys that have lower score minimum
foreach($all as $i=>$a){
    foreach($a as $n=>$scores){
        $fscores[$n]['score']=array_sum($scores);
        $fscores[$n]['farray']=min($farray[$i][$n])>=7;  // check that both ED and EN scores are >=7
        $fscores[$n]['tarray']=min(array_intersect_key($tarray[$i][$n],$sub23_keys)) && min(array_diff_key($tarray[$i][$n],$sub23_keys))>=26;
    }
}
var_export($fscores);