How can I pass an array to a function?
Let's say I have this multi-dimensional array:
$TheArray = (
array("Value 1 0","Value 1 1","Value 1 2"),
array("Value 2 0","Value 2 1","Value 2 2"),
array("Value 3 0","Value 3 1","Value 3 2")
);
Instead of doing this...
for ($i=0; $i<=(count($TheArray)-1); $i++)
{
echo $TheArray[$i][0] . " " . $TheArray[$i][1] . " " . $TheArray[$i][2] . "<br />";
}
I want to do this...
function DoStuffWithTheArray($SubArr)
{
echo $SubArr[0] . " " . $SubArr[1] . " " . $SubArr[2] . "<br />";
}
for ($i=0; $i<=(count($TheArray)-1); $i++)
{
DoStuffWithTheArray($TheArray[$i]);
}
Hopefully, you can tell what I am trying to do, but I do not know how to get it to work. When I do try it the way I want, all the values are empty
foreach
loops for array iteration whenever possible. In PHP, using an incrementalfor
loop is actually not that common for basic iteration. – Michael Berkowski Feb 1 '13 at 2:43echo 'function called<br/>';
intofunction DoStuffWithTheArray
to check if you have called the function – Michael Law Feb 1 '13 at 2:44array
at$TheArray = array(
? – Matt Whipple Feb 1 '13 at 2:47array(
keyword. codepad.viper-7.com/Ysz6qT. If that is your issue (blank screen), turn on error reporting.error_reporting(E_ALL); ini_set('display_errors', 1);
– Michael Berkowski Feb 1 '13 at 2:48