1

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
}
15
  • 4
    What's here array_map(`calc`,$cr) ? ` <- Commented May 14, 2013 at 9:15
  • In calc() you're returning $n o_o ? Shouldn't you return $x? By the way this $x = $x; does not make sense ... Commented May 14, 2013 at 9:15
  • what is the output or error message of this code? Commented May 14, 2013 at 9:18
  • @HamZaDzCyberDeV I want it return null , if $x is null , how to express it ? Commented May 14, 2013 at 9:19
  • 1
    Backticks are not valid, use quotes: array_map('calc', ...)! Commented May 14, 2013 at 9:30

2 Answers 2

3

calc should be global, otherwise it cannot be found. Also, you should pass a string (no ` but rather enclose in ' or ").

Additionally, in general (if you used PHP 5.3), it is better to pass a function reference to the array_map function, instead of a string:

$func = function calc() { ... }
array_map($func, $cr);
Sign up to request clarification or add additional context in comments.

4 Comments

See this: I am using php 5.2.10. Closure is supported on PHP 5.3 or later.
@Willem Mulder thank you for your response but i am using php 5.2.10
Yeah, so ensure that calc is global and that you use no backticks but proper quotes. Does that work?
show nothing when using proper quotes, but as what @CertaiN said may be i did have to prepare the function for array_map. If i prepared and use proper quotes, it may work. but it takes time for me to modify it.
1

I think you don't have to prepare the function for array_map.

function get_result($link_identifier = NULL) {
    $result = mysql_query('Select * from table', $link_identifier);
    $new = array();
    while ($rows = mysql_fetch_assoc($result)) {
        $r_id = '999' . $rows['batch_id'] . $rows['seq_id'];
        foreach ($rows as $k => $v) {
            if ($v !== null && preg_match('@^Item \\d+$@', $k)) {
                $v = (string)((int)$v + 1);
            }
            $new[] = array(
                'r_id' => $r_id,
                'q_id' => $rows['question_id'],
                'c_id' => $k,
                'rank' => $v,
            );
        }
    }
    return $new;
}

8 Comments

It takes time to modify i will let you know if its work or not !
@^Item \\d+$@ is different from {^Item \d+$}?
As a delimiter, { } and @ @ have same meanings. I prefer @ @ to { } because { and } have special meanings . [PHP Manual] php.net/manual/en/regexp.reference.repetition.php About \\d and \d, see these links: [PHP] ideone.com/FWzLF0 [C] ideone.com/qpSP2T Note: Most major languages behaves like C. Only PHP can behave oddly .
great !!!! thank you!!!!!! It works !! and I want to ask you one more question , what is the $link identifier used for?
Thank you very much. You gave me a detail explaination that help me alot. You english is good ! btw, I am not a english speaker also :P. I am chinese xp
|

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.