954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

2 -D ARRAYS in PHP

<?php
echo date("Y/m/d") . "";
echo date("Y.m.d") . "";
echo date("Y:->m:->d")."";
echo("hello") ."<br/>";
 $app5=5;
echo($app5);
$baby="going";
echo("$baby i love to ");
//echo ($baby$app5);
if($app5==5)
echo "dance"."";
echo strlen("dance");
echo strpos("dance","h");
switch($ab=1)
{
	case 1:
		echo 1;
	case 2:
		echo 2;
		default :
		echo "default";
}
$a = array(1,2,5,3,6);
for($i=0;$i<5;$i++)
echo $a[$i]."";
$a = array(1=>"apple",2=>"boy",3=>"girl",4=>"koel",5=>"good");
for($i=1;$i<=5;$i++)
echo $a[$i]."";
echo $a[2]."";
echo $a['2']."";
$a=array	(

$0=array(40,60,20),
$1=array(278,102,173)
);
for($i=0;$i<2;$i++)
{	echo "".$a[$i];
	for($j=0;$j<3;$j++)
	echo $a[$i][$j]." ";
}
?>

I have started learning PHP today and I was trying out some very basic commands.
I am getting an error in line 35.
How to use 2-d arrays in PHP?
Also give example of Associated 2-d array.
Any help will be appreciated.

swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
 

A variable cannot start with a number, you have to use an alphabetic character instead, so $0 and $1 are wrong. For example use $a0 and $a1.

Anyway, in your case a bi-dimensional array can be written as below:

$a = array(
 array(40,60,20),
 array(278,102,173)
);

To display data, then write:

echo $a[0][1]; # will display 60

If you want to assign specific keys you can write:

$a = array(
'alfa' => array(40,60,20),
'beta' => array(278,102,173)
);

echo $a['alfa'][2]; # output 20

bye.

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: