2-dimensional array in PHP?

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

2-dimensional array in PHP?

Post by OldRod »

I have a base_stats table that has a row for each race with 5 fields for each record. I'm wanting to read that into a 2-dimensional array in PHP so I can do some manipulation with it before using it, but I'm not having any luck.

Anyone got an example that shows how to do this?
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: 2-dimensional array in PHP?

Post by Chris »

Code: Select all

<?php

$array = array( array('foo', 'bar') );

foreach( $array as $array2 )
{
    foreach( $array2 as $array2value )
    {
        echo $array2value;  //foobar
    }
}

?>
Something like that?
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: 2-dimensional array in PHP?

Post by Jackolantern »

PHP does not natively support two-dimensional arrays like C++ does (like foo[2,1]), but does support "array or arrays" (like $foo[2][1]).
The indelible lord of tl;dr
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: 2-dimensional array in PHP?

Post by OldRod »

I've got the table read in ok, using this code:

Code: Select all

	$qrystring = "SELECT * FROM $db_race_table";
	$result = mysql_query ($qrystring) or die("Could not select from table:$db_race_table");

	$ktr = 0;
	while ($race_row = mysql_fetch_assoc($result))
	{
		foreach ($race_row as $key=>$value)
		{
			$race_stats[$ktr][$key] = $value;
		}
        $ktr++;
	}

Now I just need to know how to reference it.

How do I reference the 2nd field in the 3rd record, for example?

My c++ past tells me $race_stats[3][2] should work, but it doesn't :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: 2-dimensional array in PHP?

Post by Jackolantern »

Remember that arrays are 0 based
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: 2-dimensional array in PHP?

Post by hallsofvallhalla »

yes it starts at 0

[0,1]
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: 2-dimensional array in PHP?

Post by OldRod »

This:

echo "$race_stats[1][0]";
echo "$race_stats[1][1]";

displays the following:

Array[0]
Array[1]
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: 2-dimensional array in PHP?

Post by hallsofvallhalla »

i am not seeing where you defined $race_stats as an array

$array_stats = array(value,value,value) ect
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: 2-dimensional array in PHP?

Post by OldRod »

I have:

$race_stats=array();

at the top of the file - I just didn't get it in when I copied/pasted the code, sorry :)

I'm thinking of going a different route to accomplish this - this is getting too frustrating :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: 2-dimensional array in PHP?

Post by Jackolantern »

I could be wrong, but because PHP is a weakly typed language, I am not sure that typing

Code: Select all

$variable = array();
will make it an array. Also, to make an array of an array, wouldn't it have to be

Code: Select all

$variable = array(array());
?

However, I am not sure if it can be done like that. Later in the code, did you treat it like an array, where you would fill it like

Code: Select all

$myArray[0][0] = $variableToFill
?
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”