I am passing an array to a function and expecting the function to store values in it. Here’s my code
The Function –
function GetDetailsById ($iStudentId, $aDetailsId)
{
/* SQL */
while ($row = mysql_fetch_array($result))
{
array_push($aDetailsId, $row[0]);
}
}
Usage –
$aDetailsId = array();
$oDetailsTable->GetDetailsById("1", $aDetailsId)
When I try to do
print_r($aDetailsId)
the array shows nothing. Am I doing it the right way?
Your array needs to be passed by reference to the function ; which means the function should be defined this way :
For more informations, see Making arguments be passed by reference
Or you could have your function [**return its result**][2] — which might be better idea *(looking at the code that calls the function, you immediately know what it does)* :
And call the function :