-
- $user[0]=1;
- $user[1]="kobe";
- $user[2]=10;
- $user[3]="mac";
- $user[4] ="kobe@bb.com";
-
- echo '
'; </li>
<li>print_r($user); </li>
<li>echo ' ';
Copy code
Example: Associative array
-
- $user=array("id"=>1, "name"=>"kobe", "age"=>10, 100=>"mac", "kobe@bbb. com");
-
- for($i=0; $i<20; $i++)
- $user[]=$i;
- echo '
'; </li>
<li>print_r($user); </li>
<li>echo '< ;/pre>';
Copy code
Example: php multidimensional array
-
- $User =array(
- array('Zhang 3',93),
- array('Li 4',80),
- array('Wang 5',98)
- );
Copy Code
Example: Traversing php array elements
-
- //Define an array
- $arr = array(0=>"kobe", 1=>"mac-t", 2=>"lin");
- //Use the first method to traverse the array
- foreach ($arr as $value) {
- echo "Value: $value; ";
- }
- echo "
";
- //Use the second method Traverse the array
- foreach ($arr as $key => $value) {
- echo "Key: $key; Value: $value; ";
- }
- ?>
Copy code
2) ,
-
- //Define array
- $array = array("ar1" => array(5=>100, 3=>120, 4=>30),
- " ar2" => array(4=>"three", 9=>"four", 1=>"five"));
- //Traverse the array
- foreach ($array as $v1)
- { // bbs.it-home.org
- foreach ($v1 as $v2)
- {
- print "$v2/n";
- }
- }
- ?>
Copy code
>>> View More php introductory tutorials
|