I am using php 5.2.10 i want to do the array_map on the array and i created a function for array mapping
function get_result(){
$result = mysql_query("Select * from table");
while($cr = mysql_fetch_array($result)){
$b = array_map(`calc`,$cr);
$rr_id = $cr['batch_id'].$cr['seq_id'];
$mqrrid = '999'.$rr_id;
$question_id = $cr['question_id'];
foreach ($b as $k => $v){
if(preg_match('{^Item \d+$}',$k)){
$new_insert[] = array(
'r_id'=>$mqrrid,
'q_id' =>$q_id,
'c_id' =>$k,
'rank'=>$v
);
}
}
}
}
function calc($n){
foreach($n as $m=> &$x) {
if (preg_match('{^Item \d+$}', $m)) {
if($x == null){
$x = $x;
}else {
$x = $x - 1;
}
}
}
return $n;
}
I don't know why I cannot call the function calc in array_map.....I cannot figure out the reason.....
Can anyone help me ?
original array :( actually the output after the array_map(calc,$cr) are same as follow)
array(23) {
["batch_id"]=>
string(1) "1"
["seq_id"]=>
string(1) "1"
["question_id"]=>
string(4) "2086"
["Item 1"]=>
string(1) "1"
["Item 2"]=>
string(1) "2"
["Item 3"]=>
string(1) "3"
["Item 4"]=>
string(1) "4"
["Item 5"]=>
string(1) "5"
["Item 6"]=>
NULL
what i need is : (minus the value of Item 1 to 6 by 1, if its null just leave it ~)
array(23) {
["batch_id"]=>
string(1) "1"
["seq_id"]=>
string(1) "1"
["q_id"]=>
string(4) "2086"
["Item 1"]=>
string(1) "0"
["Item 2"]=>
string(1) "1"
["Item 3"]=>
string(1) "2"
["Item 4"]=>
string(1) "3"
["Item 5"]=>
string(1) "4"
["Item 6"]=>
NULL
Finally, the result will become like this:(example of Item 1 and Item 6)
array(4) {
["r_id"]=>
string(5) "99911"
["q_id"]=>
string(4) "2086"
["c_id"]=>
string(6) "Item 1"
["rank"]=>
string(1) "0"
}
array(4) {
["r_id"]=>
string(5) "99916"
["q_id"]=>
string(4) "2086"
["c_id"]=>
string(6) "Item 6"
["rank"]=>
string(4) NULL
}
array_map(`calc`,$cr)?`<-calc()you're returning$no_o ? Shouldn't you return$x? By the way this$x = $x;does not make sense ...array_map('calc', ...)!