1

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

7
  • What does all the values are empty mean? Do you not get any output? Commented Feb 1, 2013 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. Commented Feb 1, 2013 at 2:43
  • Try add echo 'function called<br/>'; into function DoStuffWithTheArray to check if you have called the function Commented Feb 1, 2013 at 2:44
  • aren't you missing an array at $TheArray = array(? Commented Feb 1, 2013 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); Commented Feb 1, 2013 at 2:48

3 Answers 3

1

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); $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
Sign up to request clarification or add additional context in comments.

1 Comment

I do know how to create an array. It was a typo on my part. Thanks for your help.
0

Just pass the array in:

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

That should work. Does it not?

2 Comments

This is what the OP is doing with DoStuffWithTheArray($TheArray[$i]) inside the loop.
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
0

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

Argument passing by Reference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.