Filtering two php arrays -


i have 2 arrays. have combined both arrays output total

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                )          )     ) 

the combination of above 2 arrays:

  $all = array (         [0] => array (              [1] => array (                  [ed] => 60                 [en] => 64                                  )              )         [1] => array (              [2] => array (                   [ed] => 43                   [en] => 50                   )               )         ) 

now want use first array , second array inserting condition following codes:

$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()  ); 

the above attempt works , output following (print_r($fscore)):

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

but want put more conditions , combine $tarray below:

 $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 minimum `$scores`     "tarray"=> min($scores)>='ed'? 23:26//ternary operator    );     return $all;    },    array()  ); 

i don't know how insert $tarray. stated earlier, without $tarray, works.

my attempts failed , not output expected result. in question used ed , en (score keys) save more space. minmum score expected $farray greater or equal 7 whereas minimum score expected $tarray 23/26. if score key ed in $tarray, minimum score greater or equal 23, else must 26. depending on conditions, want return true or false value. please help. below attempt:

    $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 $key =>$val){     foreach($val $k=>$v){         foreach($v $t=>$m){             if($t=='ed'){                return $m>=23;             }else{                return $m>=26;             }                     }     }   } } function filterfarray($value){     foreach($value $key =>$val){     foreach($val $k=>$v){         foreach($v $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>"; 

this outputs:

e_warning : type 2 -- invalid argument supplied foreach() 

the expected output code is:

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

i don't think i'd bother trying spice answer array_reduce() call. more important comprehensibly deliver correct comparisons , result. welcome convert series of foreach loops array functions.

my latest edit added more sample score data newly advised keys. adjustment $fscore's tarray comparison lowest non-ed score compared against 26.

code: (demo)

$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 $i=>$a){     foreach($a $n=>$scores){         foreach($scores $k=>$v){             $all[$i][$n][$k]=$v+$tarray[$i][$n][$k];  // sum relative scores         }     } } var_export($all); echo "\n\n";  // generate $fscores $sub23_keys=array_flip(['ed']);  // store list of keys have lower score minimum foreach($all $i=>$a){     foreach($a $n=>$scores){         $fscores[$n]['score']=array_sum($scores);         $fscores[$n]['farray']=min($farray[$i][$n])>=7;  // check both ed , en scores >=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); 

output:

// $all= array (   0 =>    array (     1 =>      array (       'ed' => 60,       'en' => 64,       'mt' => 44,       'mz' => 47,     ),   ),   1 =>    array (     2 =>      array (       'ed' => 43,       'en' => 50,       'mt' => 92,       'mz' => 90,     ),   ), )  //$fscores= array (   1 =>    array (     'score' => 215,     'farray' => true,     'tarray' => true,   ),   2 =>    array (     'score' => 275,     'farray' => false,     'tarray' => true,   ), ) 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -