Home >Backend Development >PHP Tutorial >PHP introductory tutorial (8) PHP array

PHP introductory tutorial (8) PHP array

WBOY
WBOYOriginal
2016-07-25 08:55:471419browse
  1. $user[0]=1;
  2. $user[1]="kobe";
  3. $user[2]=10;
  4. $user[3]="mac";
  5. $user[4] ="kobe@bb.com";
  6. echo '
    '; </li>
    <li>print_r($user); </li>
    <li>echo '
    ';
Copy code

Example: Associative array

  1. $user=array("id"=>1, "name"=>"kobe", "age"=>10, 100=>"mac", "kobe@bbb. com");
  2. for($i=0; $i<20; $i++)
  3. $user[]=$i;
  4. echo '
    '; </li>
    <li>print_r($user); </li>
    <li>echo '< ;/pre>'; 
Copy code

Example: php multidimensional array

  1. $User =array(
  2. array('Zhang 3',93),
  3. array('Li 4',80),
  4. array('Wang 5',98)
  5. );
Copy Code

Example: Traversing php array elements

  1. //Define an array
  2. $arr = array(0=>"kobe", 1=>"mac-t", 2=>"lin");
  3. //Use the first method to traverse the array
  4. foreach ($arr as $value) {
  5. echo "Value: $value; ";
  6. }
  7. echo "
    ";
  8. //Use the second method Traverse the array
  9. foreach ($arr as $key => $value) {
  10. echo "Key: $key; Value: $value; ";
  11. }
  12. ?>
Copy code

2) ,

  1. //Define array
  2. $array = array("ar1" => array(5=>100, 3=>120, 4=>30),
  3. " ar2" => array(4=>"three", 9=>"four", 1=>"five"));
  4. //Traverse the array
  5. foreach ($array as $v1)
  6. { // bbs.it-home.org
  7. foreach ($v1 as $v2)
  8. {
  9. print "$v2/n";
  10. }
  11. }
  12. ?>
Copy code

>>> View More php introductory tutorials



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn