473,387 Members | 1,423 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,387 software developers and data experts.

Multidimensional associated array

Hi,

I try to construct an array such that each element of it is another
array returned by the fetch_array_function:

for ( $i = 1; $i<=$n; $i++ )
{
$ar[$i] = mysql_fetch_array( $result );
print "$ar[$i][fieldname]\n";
}

It prints "Array[fieldname]".

If I replace $ar[$i] by $ar everything works fine! Could you pleas
help me with that?
Jan 12 '08 #1
9 2138
Kurda Yon wrote:
Hi,

I try to construct an array such that each element of it is another
array returned by the fetch_array_function:

for ( $i = 1; $i<=$n; $i++ )
{
$ar[$i] = mysql_fetch_array( $result );
print "$ar[$i][fieldname]\n";
}

It prints "Array[fieldname]".

If I replace $ar[$i] by $ar everything works fine! Could you pleas
help me with that?
print $ar[$i]['fieldname'] . "\n";
or
print "{$ar[$i]['fieldname']\n";

[fieldname] will give you an E_NOTICE if enabled (and it should be on
your development system).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jan 12 '08 #2
Jerry Stuckle wrote:
Kurda Yon wrote:
>Hi,

I try to construct an array such that each element of it is another
array returned by the fetch_array_function:

for ( $i = 1; $i<=$n; $i++ )
{
$ar[$i] = mysql_fetch_array( $result );
print "$ar[$i][fieldname]\n";
}

It prints "Array[fieldname]".

If I replace $ar[$i] by $ar everything works fine! Could you pleas
help me with that?

print $ar[$i]['fieldname'] . "\n";
or
print "{$ar[$i]['fieldname']}\n";

[fieldname] will give you an E_NOTICE if enabled (and it should be on
your development system).
A notice is not thrown in this case because the key is already within
double quotes. The problem is fixed by the necessity to use { }'s when
using multi-dimensional arrays within double quotes. I normally use this
method. To get PHP to throw a NOTICE, remove the single quotes from your
second example.

--
Norman
Registered Linux user #461062
Jan 12 '08 #3
print_r will show you the internals
Jan 13 '08 #4
no point for you to control the index yourself
<?php
$a[] = 2;
$a[] = 3;
$a[] = 5;
print $a;
print_r($a);
?>
Jan 13 '08 #5
<?php
$a = array(array("a"=>1, "b"=>2));
print "{$a[0]['b']}";
?>
Jan 13 '08 #6
you obviously forgot to close your }
Jan 13 '08 #7
<?php
$a = array(array("a"=>1, "b"=>2));
print "{$a[0][b]}";
?>
this gives you notice,
Jan 13 '08 #8
php ends the variable name at the most convenience spot
Jan 13 '08 #9
Peter Pei wrote:
<?php
$a = array(array("a"=>1, "b"=>2));
print "{$a[0][b]}";
?>
this gives you notice,
Yes, that's what I said. When using multi-dimensional arrays within
double-quoted strings you must use the { }'s... and when you use the {
}'s you must use single quotes around the key names. PHP does not search
for CONSTANTS within double-quoted strings, there is a very specific
reason for it.
--
Norman
Registered Linux user #461062
Jan 14 '08 #10

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

Similar topics

9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
7
by: bijax | last post by:
hi, i am new to multidimensional array of c programming .pls help me to solve out this how to allocate memory for mutidimensional array? i.e a ;
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
1
by: Chuy08 | last post by:
If I have a multidimensional array like the following: Array $records =Array 0 = 30 year, 6.0; 1 = 30 year, 6.0; 2 = Pay Option, 1.0; 3 = Pay Option, 1.0; How could I flatten this to...
5
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30...
4
Jezternz
by: Jezternz | last post by:
First of all I am open to any suggestions and advice. If a javscript multidimensional array is a bad way to do this please say so. I considered XML but I wondered if this would be a bad idea as it...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...

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.