473,662 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning a 2 dimensional array

I am trying to program a function to return a 2 dimensional array, but
it's not working. I reduced the return value to 1 dimension and
tested that to make sure that the problem wasn't elsewhere. It had no
problem when the array was 1 dimensional.

Is there something different I need to do in order to return a 2
dimensional array from a function?
Nov 3 '05 #1
20 14158
JDS
On Thu, 03 Nov 2005 12:10:55 -0500, Parrot wrote:
Is there something different I need to do in order to return a 2
dimensional array from a function?


Yes. Create a 2-D array inside the function. Return it.

As simple as this:

function get_2d_array(){
return array(array());
}

Maybe your function is not *creating* a 2D array?
--
JDS | je*****@example .invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Nov 3 '05 #2
I definitely created a 2D array, I tested it within the function.

Your example doesn't give an example of creating a 2D array and I
don't see any variable names in your return statement. Could you
please expand on that?

On Thu, 03 Nov 2005 14:48:19 -0500, JDS <je*****@exampl e.invalid>
wrote:
On Thu, 03 Nov 2005 12:10:55 -0500, Parrot wrote:
Is there something different I need to do in order to return a 2
dimensional array from a function?


Yes. Create a 2-D array inside the function. Return it.

As simple as this:

function get_2d_array(){
return array(array());
}

Maybe your function is not *creating* a 2D array?


Nov 3 '05 #3
Parrot (me@myplace.com ) TOP POSTED:
: I definitely created a 2D array, I tested it within the function.

Let's see the code.

: Your example doesn't give an example of creating a 2D array and I

It looks like an array of arrays to me. Perhaps you mean something else
when you say "2D array".

: don't see any variable names in your return statement. Could you
: please expand on that?

What does the name of the variable have to do with this? He returns a php
"thing" which is an array of arrays. You could have assigned that "thing"
to a variable first if you wished, and then returned the value of the
thing by using the variable. I will admit I can not test that at the
moment, but find it hard to imagine it doesn't work as I expect...

$two_D_array = array(array());
return $two_D_array;

We need to see your code to debug the error or misunderstandin g.
: On Thu, 03 Nov 2005 14:48:19 -0500, JDS <je*****@exampl e.invalid>
: wrote:

: >On Thu, 03 Nov 2005 12:10:55 -0500, Parrot wrote:
: >
: >> Is there something different I need to do in order to return a 2
: >> dimensional array from a function?
: >
: >Yes. Create a 2-D array inside the function. Return it.
: >
: >As simple as this:
: >
: >function get_2d_array(){
: > return array(array());
: >}
: >
: >Maybe your function is not *creating* a 2D array?
--

This programmer available for rent.
Nov 3 '05 #4
On 3 Nov 2005 12:57:34 -0700, yf***@vtn1.vict oria.tc.ca (Malcolm
Dew-Jones) wrote:

We need to see your code to debug the error or misunderstandin g.


Okay:
function maincats()
{
global $datHost;
global $datUser;
global $datPass;
global $datName;

$db1 = mysql_connect($ datHost,$datUse r,$datPass) or die
("Unable to connect: ". mysql_error());
mysql_select_db ($datName);

$qstr="select * from categories where parent_id='0'";
$qry=mysql_quer y($qstr) or die ("Unable to set query:
$qstr<BR>". mysql_error());
$count = 0;
while($arr=mysq l_fetch_array($ qry))
{
$cat[$count][0] = $arr['cat_id'];
$cat[$count][1] = $arr['name'];
$count += 1;
}
return $cat;
}
Nov 3 '05 #5
Don't see anything wrong with the code. I suspect the problem lies in
the way you access the array. Do a print_r() on the return value and
see if you're getting what you're supposed to.

Nov 3 '05 #6
On 3 Nov 2005 14:28:49 -0800, "Chung Leong"
<ch***********@ hotmail.com> wrote:
Don't see anything wrong with the code. I suspect the problem lies in
the way you access the array. Do a print_r() on the return value and
see if you're getting what you're supposed to.


I set up a simple test script to try and figure out how to work the
arrays:

<?
function test()
{
$array[0][0] = "Hello";
$array[0][1] = "World";
$array[1][0] = "How Are";
$array[1][1] = "You?";

return array($array);
}

$array = test();
echo $array[0][0];
?>

When I run it it just prints out the word "Array".

When I replace the "echo $array[0][0];" line with "print_r ($array);"
it gives me:

Array ( [0] => Array ( [0] => Array ( [0] => Hello [1] => World ) [1]
=> Array ( [0] => How Are [1] => You? ) ) )

Is it returning a 3 dimensional array or something??
Nov 3 '05 #7
Parrot wrote:
On 3 Nov 2005 14:28:49 -0800, "Chung Leong"
<ch***********@ hotmail.com> wrote:

Don't see anything wrong with the code. I suspect the problem lies in
the way you access the array. Do a print_r() on the return value and
see if you're getting what you're supposed to.

I set up a simple test script to try and figure out how to work the
arrays:

<?
function test()
{
$array[0][0] = "Hello";
$array[0][1] = "World";
$array[1][0] = "How Are";
$array[1][1] = "You?";

return array($array);
}

$array = test();
echo $array[0][0];
?>

When I run it it just prints out the word "Array".

When I replace the "echo $array[0][0];" line with "print_r ($array);"
it gives me:

Array ( [0] => Array ( [0] => Array ( [0] => Hello [1] => World ) [1]
=> Array ( [0] => How Are [1] => You? ) ) )

Is it returning a 3 dimensional array or something??

I tend to go this site a lot!

<http://uk.php.net/types.array>

Richard.
Nov 4 '05 #8
Ah, you got confused by JDS's cryptic message. To return an array, you
just return it. A call to array() would create a new array with
whatever passed as its elements. For example, the following would give
you an array with 2 elements, "Hello" and "World".

$array = array("Hello", "World");

The following is what you need to create a array of arrays:

function test()
{
$array[0][0] = "Hello";
$array[0][1] = "World";
$array[1][0] = "How Are";
$array[1][1] = "You?";
return $array;
}

I said "array of arrays" instead of "2-D array" because that's what
they are. Strictly speaking, the function doesn't create one 2-D array;
it creates three arrays--two inside the third. We could've created the
same structure with the following:

function test()
{
$array = array(
array("Hello", "World"),
array("How are", "You?")
);
return $array;
}

or

function test()
{
$array = array();
$array[] = array("Hello", "World");
$array[] = array("How are", "You?");
return $array;
}

Personally I prefer the last syntax, as it's more obvious what's
happening: (1) an empty array is created (2) a new array with two
strings is added (3) another array with two strings is added.

Now going back to your code, you could have coded the while loop like
this

while($arr=mysq l_fetch_array($ qry))
{
$cat[] = array($arr['cat_id'], $arr['name']);
}

Nov 4 '05 #9
Parrot wrote:
On 3 Nov 2005 14:28:49 -0800, "Chung Leong">
<ch***********@ hotmail.com> wrote:

Don't see anything wrong with the code. I suspect the problem lies in
the way you access the array. Do a print_r() on the return value and
see if you're getting what you're supposed to.

I set up a simple test script to try and figure out how to work the
arrays:

<?
function test()
{
$array[0][0] = "Hello";
$array[0][1] = "World";
$array[1][0] = "How Are";
$array[1][1] = "You?";

return array($array);
}

$array = test();
echo $array[0][0];
?>

When I run it it just prints out the word "Array".


Doing a Google search I came across this URL but the interesting thing
was the last line.

"Multi-dimensional arrays

PHP does not explicitly support multi-dimensional arrays. However PHP
allows arrays of any type of object, including arrays so two dimensional
arrays can be implemented as arrays of arrays. "

<http://www.scit.wlv.ac .uk/~jphb/sst/php/lang/phparrays.html>
Richard.
Nov 4 '05 #10

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

Similar topics

10
3155
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I have commented the problem parts. Thanks in advance for any help offered. Pete
8
1869
by: shan | last post by:
How to return an two dimensional array in user defined function to main function. I think it is posible by using pointers but don't know how to code.or is there any method without using pointers.It is just a queston arised by me not homework,I specify this because in my previous posts I get replied by such comments.
60
10142
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: > http://groups.google.com/group/comp.lang.c++/msg/db577c43260a5310?hl > >Another way is to create a one dimensional array and handle the >indexing yourself (index = row * row_size + col). This is readily >implemented in template classes that can create dynamically allocated >multi-dimensional...
22
2275
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest that it will be evaluated like "x", just as "x" is evaluated like "x" right now.
6
2269
by: fniles | last post by:
I need to store information in a 2 dimensional array. I understand ArrayList only works for a single dimensional array, is that correct ? So, I use the 2 dimensional array like in VB6. I pass the array into a subroutine, and inside it I "Redim Preserve" the array to increase the number of item in the array. I got the error "Redim statement requires an array", but arr is an array. HOw can I fix this problem ? Or, how can I do ArrayList for...
8
11820
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious way to do this is a nested for-loop - but we all know O(n^2) is bad. So I am looking for something like ArrayList.ToArray(), or Matlabs A(:). C#
272
14011
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
13
2545
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns data from a query into an array. The intent is that the following code:
5
3879
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six different models of cars that are produced by the car manufacturer. Task 1 Step 1: Create a directory called Assignment02 and create the files of steps 2, 3 and 4 in this directory as well as your project file. Step 2: Create a file called...
0
8435
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.