Connecting Tech Pros Worldwide Forums | Help | Site Map

Multi-Dimensional Arrays

Chad Lupkes
Guest
 
Posts: n/a
#1: Jul 19 '05
Hoping someone can help me here. It's a simple question, but I'm just not
getting it.

I have two files:

<?php
$precinct = array (
array ( number=>2324,
centerlat=>-122.31705665588379,
centerlong=>47.710367959402525,
etc...

<?php
include ("precinctdata.php");
$filename = $_GET['id'];
$number = $precinct[filename-1][number];
etc...

I want to assign the value of "2324" to the variable $number. I've done
this with arrays that start with number=>1, 2, 3, etc., but not successfully
with specific values.

What am I missing?

--
Chad Lupkes
chadlupkes@gmail.com
http://www.seattlewebcrafters.com/


Jerry Stuckle
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Multi-Dimensional Arrays


Chad Lupkes wrote:[color=blue]
> Hoping someone can help me here. It's a simple question, but I'm just not
> getting it.
>
> I have two files:
>
> <?php
> $precinct = array (
> array ( number=>2324,
> centerlat=>-122.31705665588379,
> centerlong=>47.710367959402525,
> etc...
>
> <?php
> include ("precinctdata.php");
> $filename = $_GET['id'];
> $number = $precinct[filename-1][number];
> etc...
>
> I want to assign the value of "2324" to the variable $number. I've done
> this with arrays that start with number=>1, 2, 3, etc., but not successfully
> with specific values.
>
> What am I missing?
>[/color]

Try
$precinct = array (
array ('number'=>2324,
'centerlat'=>-122.31705665588379,
'centerlong'=>47.710367959402525,
...
$number = $precinct[$filename-1]['number'];
^ ^ ^

filename is a variable so it needs a $. number is a string, so it needs single
quotes. The same is true when you're creating the array.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Geoff Berrow
Guest
 
Posts: n/a
#3: Jul 21 '05

re: Multi-Dimensional Arrays


I noticed that Message-ID:
<Ch_Ce.205$Uk3.89@newsread1.news.pas.earthlink.net > from Chad Lupkes
contained the following:
[color=blue]
>$precinct = array (
> array ( number=>2324,
> centerlat=>-122.31705665588379,
> centerlong=>47.710367959402525,
>etc...
>
><?php
>include ("precinctdata.php");
>$filename = $_GET['id'];
>$number = $precinct[filename-1][number];
>etc...[/color]

You haven't created an associative array.

The array key can be a number, as you have used before, or a string. If
you use a string you don't get a number as well. The only thing you
could do in this instance is assign the array to another array with
numerical indices.

eg
foreach($precinct as $value){
$precinct_num[]=$value;
}

then

$filename = $_GET['id'];
$number = $precinct_num[$filename-1];

However, I suggest you look at the overall logic of what you are trying
to do.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
chadlupkes
Guest
 
Posts: n/a
#4: Jul 21 '05

re: Multi-Dimensional Arrays


Hi Geoff,

Ok, what I'm trying to make happen is that when people type in or click
on a link that takes them to precinctmap.php?id=2324, it will pull the
data necessary to draw a map of that precinct. In the other arrays
that I have built similar to this, I have used numerical values
starting with 1. For example, in Washington State we have 9
Congressional Districts, 49 Legislative Districts and 39 Counties.
Each of the scripts that pull from these arrays work fine, because I'm
able to use array values of 0-8, 0-48 and 0-38 with the 'filename-1'
reference.

For the application I am trying to create now, the precinct numbers
start at 1281, and skip/jump up to 3423. It's not a sequential set of
numbers. There are 224 of them, so I could create the array and
display script to use key values of 0-223 if I have to, but I'd rather
be able to call the specific data I need based on the precinct number
instead of an arbritary number that only works when I know the key
value. That's why I'm trying to use strings.

I know a database might be a better way of doing this, but I have yet
to get a good understanding of how to use databases, so I'm trying to
work with what I know.

Does that description help? I've been trying to find a tutorial page
that explains how to call string values from arrays, but I can't find
one that I really understand.

Chad

JDS
Guest
 
Posts: n/a
#5: Jul 21 '05

re: Multi-Dimensional Arrays


On Mon, 18 Jul 2005 23:23:26 -0500, Jerry Stuckle wrote:
[color=blue]
> JDS Computer Training Corp.[/color]

Those are my initials!

--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Geoff Berrow
Guest
 
Posts: n/a
#6: Jul 21 '05

re: Multi-Dimensional Arrays


I noticed that Message-ID:
<1121780533.445057.291240@g43g2000cwa.googlegroups .com> from chadlupkes
contained the following:
[color=blue]
>Does that description help? I've been trying to find a tutorial page
>that explains how to call string values from arrays, but I can't find
>one that I really understand.[/color]

Ah yes gotcha. You want the precinct number to be the key to another
array. Try this:

$precinct = array(
2324=> array (
centerlat=>-122.31705665588379,
centerlong=>47.710367959402525,
)
);
print $precinct[2324]['centerlat'];
//prints -122.31705665588379
print"<br>";
print $precinct[2324]['centerlong'];
//prints 47.710367959402525


I have a very basic tutorial here
http://www.ckdog.co.uk/phpcourse/3_arrays_and_loops.doc
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jerry Stuckle
Guest
 
Posts: n/a
#7: Jul 21 '05

re: Multi-Dimensional Arrays


JDS wrote:[color=blue]
> On Mon, 18 Jul 2005 23:23:26 -0500, Jerry Stuckle wrote:
>
>[color=green]
>>JDS Computer Training Corp.[/color]
>
>
> Those are my initials!
>[/color]

Mine, too! :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Shelly
Guest
 
Posts: n/a
#8: Jul 21 '05

re: Multi-Dimensional Arrays



"chadlupkes" <chadlupkes@gmail.com> wrote in message
news:1121780533.445057.291240@g43g2000cwa.googlegr oups.com...[color=blue]
> Hi Geoff,
>
> Ok, what I'm trying to make happen is that when people type in or click
> on a link that takes them to precinctmap.php?id=2324, it will pull the
> data necessary to draw a map of that precinct. In the other arrays
> that I have built similar to this, I have used numerical values
> starting with 1. For example, in Washington State we have 9
> Congressional Districts, 49 Legislative Districts and 39 Counties.
> Each of the scripts that pull from these arrays work fine, because I'm
> able to use array values of 0-8, 0-48 and 0-38 with the 'filename-1'
> reference.
>
> For the application I am trying to create now, the precinct numbers
> start at 1281, and skip/jump up to 3423. It's not a sequential set of
> numbers. There are 224 of them, so I could create the array and
> display script to use key values of 0-223 if I have to, but I'd rather
> be able to call the specific data I need based on the precinct number
> instead of an arbritary number that only works when I know the key
> value. That's why I'm trying to use strings.
>
> I know a database might be a better way of doing this, but I have yet
> to get a good understanding of how to use databases, so I'm trying to
> work with what I know.
>
> Does that description help? I've been trying to find a tutorial page
> that explains how to call string values from arrays, but I can't find
> one that I really understand.
>
> Chad
>[/color]

How about rather than the_array[$i], using the_array["$i"]? The index is
then a string rather than a number. You will only have the 224 elements,
but their names can be "1281" or "3423". This seem like the simplest
solution to you problem.

Shelly


Closed Thread