Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
What does all the values are empty mean? Do you not get any output? –  Joseph Silber Feb 1 '13 at 2:38
    
Get in the habit of using foreach loops for array iteration whenever possible. In PHP, using an incremental for loop is actually not that common for basic iteration. –  Michael Berkowski Feb 1 '13 at 2:43
    
Try add echo 'function called<br/>'; into function DoStuffWithTheArray to check if you have called the function –  Michael Law Feb 1 '13 at 2:44
    
aren't you missing an array at $TheArray = array(? –  Matt Whipple Feb 1 '13 at 2:47
    
This works exactly as you have it, except for the missing array( 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

3 Answers 3

up vote 1 down vote accepted

I think, you need to learn first how create an array;

// instead of this
$TheArray = (...
// you can create an array so
$TheArray = array(...

Second, change this style please and see: http://php.net/manual/en/control-structures.foreach.php

for ($i=0; $i<=(count($TheArray)-1); $i++)

And answer;

$array = array(
    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")
);

function fn($a) {
    print "$a[0], $a[1], $a[1]\n";
}

foreach ($array as $a) fn($a);
Value 1 0, Value 1 1, Value 1 1
Value 2 0, Value 2 1, Value 2 1
Value 3 0, Value 3 1, Value 3 1
share|improve this answer
    
I do know how to create an array. It was a typo on my part. Thanks for your help. –  Soren Feb 1 '13 at 16:32

Just pass the array in:

function DoStuffWithTheArray($array) {
    $array[0]...$array[n];
}

That should work. Does it not?

share|improve this answer
1  
This is what the OP is doing with DoStuffWithTheArray($TheArray[$i]) inside the loop. –  Michael Berkowski Feb 1 '13 at 2:44
    
The second example works for me. You do need to make the following correction: $TheArray = array(.... But when I past in the array and the second section of code, it works just fine on writecodeonline.com/php –  mrunion Feb 1 '13 at 2:50

If you want to alter the content of the array, then you need to pass it by reference:

Argument passing by Reference

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.