user2210819
user2210819

Reputation: 145

Passing array from a function to another function

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 ?

Upvotes: 0

Views: 46

Answers (2)

Suvash sarker
Suvash sarker

Reputation: 3170

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
    //}
}

Upvotes: 1

davidkonrad
davidkonrad

Reputation: 85518

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
}

Upvotes: 1

Related Questions