0
<?php
echo date("Y/m/d") . "<br />";
echo date("Y.m.d") . "<br />";
echo date("Y:->m:->d")."<br>";
echo("hello") ."<br/>";
 $app5=5;
echo($app5);
$baby="going";
echo("$baby i love to ");
//echo ($baby$app5);
if($app5==5)
echo "dance"."<br>";
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]."<br>";
$a = array(1=>"apple",2=>"boy",3=>"girl",4=>"koel",5=>"good");
for($i=1;$i<=5;$i++)
echo $a[$i]."<br>";
echo $a[2]."<br>";
echo $a['2']."<br>";
$a=array	(

$0=array(40,60,20),
$1=array(278,102,173)
);
for($i=0;$i<2;$i++)
{	echo "<br>".$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.

Edited by swissknife007: n/a

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by cereal
2

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.

Votes + Comments
Nicely explained
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.