Jump to content

array in functions


iamnoguru
 Share

Recommended Posts

I have an array in a file two dimensional one for($k=0;$k<$num;$k++) for($i=0;$i<$num1;$i++) { $id1[$k][$i]=mysql_result($result3,$i,"e_id");$id[$k][$i]=mysql_result($result3,$i,"1_id");$id2[$k][$i]=mysql_result($result3,$i,"2_id");}}Now I want pass this valuses to a functionhow do i do it;And I want to pass for every k

Link to comment
Share on other sites

You can use array_walk or array_map to pass the values of an array to a function.If you want to pass it for each $k, just put it before or after the $i loop.
include 'functionstouse.php';mysql_data_seek($result, 0);$k=0;$num = mysql_numrows($result);while ($row1 = mysql_fetch_array($result)) { $query3 = 'SELECT DISTINCT p.id,r.relationship_type_id,r.term1_id,r.term2_id FROM graph_path INNER JOIN term AS t ON (t.id = graph_path.term2_id) INNER JOIN term AS p ON (p.id = graph_path.term1_id) INNER JOIN term2term AS r ON (r.term2_id = p.id) WHERE t.acc="'.$row1['acc'].'" ORDER BY graph_path.distance ASC'; $result3 = mysql_query($query3) or die("Query Failed! " . mysql_error());$num1 = mysql_numrows($result3);echo "<br/><br/>result3 rows: ".$num1."<br/>";$relid = array();$term1id = array();$term2id = array(); for($i=0;$i<$num1;$i++) { $relid[$k][$i]=mysql_result($result3,$i,"relationship_type_id");$term1id[$k][$i]=mysql_result($result3,$i,"term1_id");$term2id[$k][$i]=mysql_result($result3,$i,"term2_id");}echo $k;//for($k=0;$k < $num; $k++){for ($i = 0; $i < $num1; $i++) {echo $relid[$k][$i]. "<br>";echo $term1id[$k][$i]. "<br>";echo $term2id[$k][$i]. "<br>";//}}$mygeneresult = array();for ($i = 0; $i < $num1; $i++) { $results = array(); $current = $term2id[$k][$i]; echo "current is ".$current; computeSval($current,$term1id[$k][], $term2id[$k][],$relid[$k][],1,$results,$k); $mygeneresult[] = $results; echo "<br>S value DAG for ".$term2id[$k][$i].": ".array_sum($results)."</br>"; echo "No of terms in the DAG: ".sizeof($term2id[$k][0])."<br>"; print_r($results);}$k++;}And the funtion is<?function computeSval($current,&$term1id,&$term2id,&$relid, $currentSval, &$results,$k) {echo "In function <br>";print_r(array_values($term1id));echo $k;echo "In function <br>"; if ($key = array_search($current, $results)) { if ($results[$key] < $currentSval) { $results[$key] = $currentSval;} } else { $results[$current] = $currentSval; } for ($i = 0; $i < 32; $i++) { if ($term2id[$k][$i] == $current) { if ($relid[$k][$i] == 2) $factor = 0.8; else $factor = 0.5; computeSval($term1id[$k][$i],$term1id[$k][],$term2id[$k][],$relid[$k][],$currentSval*$factor,$results); } }}?>but i dont see any values in term1id or term2id in the fuction
Link to comment
Share on other sites

i got a new problem, what if i want to return multiple things from one function.function makearray($result,$k){$num1 = mysql_numrows($result);echo "<br/><br/>result3 rows: ".$num1."<br/>";$relid = array();$term1id = array();$term2id = array(); for($i=0;$i<$num1;$i++) { $relid[$k][$i]=mysql_result($result,$i,"relationship_type_id");$term1id[$k][$i]=mysql_result($result,$i,"term1_id");$term2id[$k][$i]=mysql_result($result,$i,"term2_id");}$array =array($relid,$term1id,$term2id);return $array;}i want to return relid,term1id, term2id but i am not able to do this way..

Link to comment
Share on other sites

I haven't done this with PHP, but in other languages, you can pass the parameters by reference rather than by value. If you pass a variable by reference as a parameter to a function, and you modify that parameter in the function, the variable outside of the function will be modified and there would be no need to return it to the caller.Check out this site: http://www.adp-gmbh.ch/php/pass_by_reference.htmlOr, this Google search: http://www.google.com/search?hl=en&lr=...ssing+reference

Link to comment
Share on other sites

I haven't done this with PHP, but in other languages, you can pass the parameters by reference rather than by value. If you pass a variable by reference as a parameter to a function, and you modify that parameter in the function, the variable outside of the function will be modified and there would be no need to return it to the caller.Check out this site: http://www.adp-gmbh.ch/php/pass_by_reference.htmlOr, this Google search: http://www.google.com/search?hl=en&lr=...ssing+reference
Thanks that worked
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...