Page 1 of 1

Returning arrays from a function in PHP

Posted: Thu Mar 25, 2004 7:06 am
by mjseaden
Dear All

If I have a function return_strings($itemID), can I make it return an array of values? For example, could I do this -

$string1 = return_strings(5)[1];
$string2 = return_strings(5)[2];
$string3 = return_strings(5)[3];

It doesn't seem intuitively that this would be the case - in which case, is there any other way of doing this?

Many thanks

Mark

Posted: Thu Mar 25, 2004 7:50 am
by mjseaden
/bump

Posted: Thu Mar 25, 2004 7:51 am
by mjseaden
It doesn't work by the way - it produces a parse error when I attempt to call the function with the above syntax.

Posted: Thu Mar 25, 2004 7:52 am
by CoderGoblin
You can have functions return an array although I would try to create the array with a meaningful index:

Code: Select all

function getArray() {
    $retval=array("name"=>"CoderGoblin","desc","PHP Coder");
    return $retval;
}

// Get result of array
$my_array=getArray();

// process the individual columns
$my_name=$my_arrayї'name'];
$my_desc=$my_arrayї'desc'];
using

Code: Select all

$string1 = return_strings(5)ї1];
$string2 = return_strings(5)ї2];
$string3 = return_strings(5)ї3];
would call the function 3 times.

Posted: Thu Mar 25, 2004 8:07 am
by mjseaden
Many thanks CoderGoblin - you're a star!