0

I have an array ,$idarray .

array(3) {
  [0]=>
  string(4) "102"
  [1]=>
  string(4) "211"
  [2]=>
  string(4) "421"
}

Call the other function an pass the array element.

foreach($idarray as $id){
$r = get_rate($id);
  foreach($r as $result=> $row){
  }
}

Another function

function get_rate($qid){
foreach($qids as $qid){
execute sql here
}

Error occur when i pass the array to the function "foreach($qids as $qid)" , then I cannot go into the foreach statement and do the execution. How can I pass an array to the function ?

1
  • $r = get_rate($id); here you are passing an array element not an array!! Commented Oct 4, 2013 at 9:31

2 Answers 2

1
foreach($idarray as $id){
  $r = get_rate($id);
  ...

passes a string, not an array. As you can see - your array contain strings, not arrays. It is not a multidimensional array. Using a string as it were an array will produce the error. Therefore :

function get_rate($qid){
   foreach($qids as $qid){
     execute sql here
   }
}

should be

function get_rate($qid){
   execute sql here, you already have a single id, $qid
}
Sign up to request clarification or add additional context in comments.

Comments

1
foreach($idarray as $id){
$r = get_rate($id);//here $id is an array element not an array
  foreach($r as $result=> $row){
  }
}

Another function

    function get_rate($qid){
      //here $qid is a single element like 102,211,so on.....
//no need foreach loop here,just execute your sql and return your result
    //foreach($qids as $qid){
    execute sql here
    //}
}

2 Comments

in this case it can get_rate for once , how about the other two ?how to execute?
If you run this code : $idarray = array(101,102,103,104); foreach($idarray as $id){ $r = get_rate($id); } function get_rate($qid){ echo 'Id:'.$qid.','; } you output will be like that:Id:101,Id:102,Id:103,Id:104,

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.