473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function returning an array ??

Not getting what I expect when I try to return an array from my
function. See sample below.

function test()
{
$myArray["one"] = 1;
$myArray["two"] = 2;
return $myArray;
}

$thisArray = test();
foreach($thisArray as $thisKey -> $thisVal)
{
echo($thisKey . " is " . $thisVal . "<br>");
}

// The output:
// Should Be: But Is:
// one is 1 Object is
// two is 2 Object is

What's up? How do I pass back an array from a function? I tried
passing it back as an &argument but that doesn't work either.
Jul 16 '05 #1
2 14358
On 18 Aug 2003 12:56:32 -0700, sa*******@yahoo.com (Texas Guy) wrote:
Not getting what I expect when I try to return an array from my
function. See sample below.

function test()
{
$myArray["one"] = 1;
$myArray["two"] = 2;
return $myArray;
}

$thisArray = test();
foreach($thisArray as $thisKey -> $thisVal)
{
echo($thisKey . " is " . $thisVal . "<br>");
}

// The output:
// Should Be: But Is:
// one is 1 Object is
// two is 2 Object is

What's up? How do I pass back an array from a function? I tried
passing it back as an &argument but that doesn't work either.
Output is in fact:
Notice: Undefined variable: thisVal in D:\public_html\test.php on line 10

Notice: Undefined variable: thisVal in D:\public_html\test.php on line 12
Object is

Notice: Undefined variable: thisVal in D:\public_html\test.php on line 10

Notice: Undefined variable: thisVal in D:\public_html\test.php on line 12
Object is

Having error_reporting set to E_ALL gives you more clues to work with.

Your problem is the loop, you're using the wrong operator:
foreach($thisArray as $thisKey -> $thisVal)


Should be:

foreach($thisArray as $thisKey => $thisVal)

-> is object access, => is for 'key => value' in foreach and array
construction.

Output with => is:

one is 1
two is 2

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #2
Texas Guy wrote:
foreach($thisArray as $thisKey -> $thisVal)

^^

Mind your syntax: You're trying to access an element in an Object here.
try "=>" instead and it works.

Jochen

--
/**
* @author Jochen Buennagel <zang at buennagel dot com>
* @see http://www.sourceforge.net/projects/zang
*/

Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
1
by: Guha | last post by:
I have a problem with returning a 2D array using a function which is called in main(). The piece of the code is given below. This is a test code only. #include"stdio.h" #include"alloc.h" ...
2
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
1
by: vijay.gandhi | last post by:
Hello, I have created a function in C++/CLI which was exported as a .DLL to be used in VB .NET. I have been having some problems (I think it has to do with the right syntax) with parameter...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
13
by: Sri Harsha Dandibhotla | last post by:
Hello all. I recently came across a function declaration as : char(*(*x()))(); This was not in some code but it was in a C questions thread on some group. I tried to decipher what it returns but...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.