php - how to do the calculation in an array -


i have array :

array (     [0] => array   (       [batch_id] => 1       [seq_id] => 1       [q_id] => 2046       [a1] => 0       [a2] => 1       [a3] => 2       [a4] => 3       [a5] => 4    ) ) 

i need minus value of a1-a5 1

desire result(e.g. a1):

 array(4) { ["w_id"]=> string(5) "99911" ["q_id"]=> string(4) "2046" ["c_id"]=> string(6) "a1" ["rank"]=> int(1) "-1" } 

my code follow:

$result = mysql_query("select * table_1");      while($cr = mysql_fetch_array($result)){         $rr_id = $cr['batch_id'].$cr['seq_id'];     $rid = '999'.$rr_id;           $q_id = $cr['q_id'];      foreach ($cr $k => $v){   if(preg_match('{^a\d+$}',$k)){     $new_insert[] = array(         'w_id'=>$rid,         'q_id' =>$q_id,         'c_id' =>$k,         'rank'=>$v-1 );      }   } 

however, result of rank becomes

array(4) { ["w_id"]=> string(5) "99911" ["q_id"]=> string(4) "2046" ["c_id"]=> string(6) "a1" ["rank"]=> int(0) } 

cannot show value of rank

any problem code??can answer question thank much

you can use :

$data = array_map(function ($v) {     foreach($v $k => &$x) {         if (preg_match('{^a\d+$}', $k)) {             $x = $x - 1;         }     }     return $v; }, $data);  print_r($data); 

live demo


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -