PHP數(shù)組多維

2018-02-22 16:40 更新

PHP教程 - PHP數(shù)組多維

多維數(shù)組可以有數(shù)組作為其值。

包含其他數(shù)組的數(shù)組是二維數(shù)組。如果這些數(shù)組也包含數(shù)組,那么頂層數(shù)組是一個(gè)三維數(shù)組,依此類推。

以下代碼使用數(shù)組運(yùn)算符([])創(chuàng)建二維陣列。 第一級(jí)的關(guān)鍵是Java和PHP。


<?PHP
$book["Java"] = array("Name"=>"Java Book", "Price"=> 1.2, "OnSale"=>"No");
$book["PHP"] = array("Name"=>"PHP Book", "Price"=>5.3, "OnSale"=>"Yes");
var_dump($book);
?>

上面的代碼生成以下結(jié)果。



示例 - 使用array()函數(shù)創(chuàng)建二維數(shù)組

以下腳本創(chuàng)建一個(gè)稱為的簡(jiǎn)單二維數(shù)組$ myBooks,然后使用print_r()顯示其內(nèi)容。


<?php /*  ww w  .j  a va2 s.  co  m*/
  $myBooks = array( 
    array( 
      "title" =>  "Learn PHP from m.hgci.cn", 
      "author" =>  "m.hgci.cn", 
      "pubYear" =>  2000 
    ), 
    array( 
      "title" =>  "Learn Java from m.hgci.cn", 
      "author" =>  "JavaAuthor", 
      "pubYear" =>  2001 
    ), 
    array( 
      "title" =>  "Learn HTML from m.hgci.cn", 
      "author" =>  "HTMLAuthor", 
      "pubYear" =>  2002 
    ), 
    array( 
      "title" =>  "Learn CSS from m.hgci.cn", 
      "author" =>  "CSSAuthor", 
      "pubYear" =>  2003 
    ), 
  ); 

  print_r ( $myBooks ); 
?>  

上面的代碼生成以下結(jié)果。



多維數(shù)組中的PHP訪問(wèn)元素

方括號(hào)語(yǔ)法可以訪問(wèn)a中的任何元素多維數(shù)組。

訪問(wèn)第一個(gè)維度

$multidimensionalArray[]

訪問(wèn)內(nèi)部尺寸

$multidimensionalArray[][]

這里有些例子


<?php /*from w  ww  . j ava2  s  .  c o  m*/
  $myBooks = array( 
    array( 
      "title" =>  "Learn PHP from m.hgci.cn", 
      "author" =>  "m.hgci.cn", 
      "pubYear" =>  2000 
    ), 
    array( 
      "title" =>  "Learn Java from m.hgci.cn", 
      "author" =>  "JavaAuthor", 
      "pubYear" =>  2001 
    ), 
    array( 
      "title" =>  "Learn HTML from m.hgci.cn", 
      "author" =>  "HTMLAuthor", 
      "pubYear" =>  2002 
    ), 
    array( 
      "title" =>  "Learn CSS from m.hgci.cn", 
      "author" =>  "CSSAuthor", 
      "pubYear" =>  2003 
    ), 
  ); 
  print_r( $myBooks[1] ); 
  echo $myBooks[1]["title"] . "\n"; 
  echo $myBooks[3]["pubYear"] . "\n";   
?>

上面的代碼生成以下結(jié)果。

PHP循環(huán)通過(guò)多維數(shù)組

多維數(shù)組基本上是嵌套在其他數(shù)組內(nèi)的數(shù)組,我們可以使用嵌套循環(huán)通過(guò)多維數(shù)組!

我們可以使用以下嵌套的foreach語(yǔ)句循環(huán)通過(guò)多維數(shù)組。

foreach ( $myBooks as $book ) { 
   foreach ( $book as $key => $value ) { 

   } 
} 

以下示例使用兩個(gè)嵌套的foreach循環(huán)循環(huán)遍歷$ myBooks數(shù)組。


<?php /* w  w w. java2s  .  c  o  m*/
   $myBooks = array( 
     array( 
       "title" => "Learn PHP from m.hgci.cn", 
       "author" => "m.hgci.cn", 
       "pubYear" => 2000 
     ), 
     array( 
       "title" => "Learn Java from m.hgci.cn", 
       "author" => "JavaAuthor", 
       "pubYear" => 2001 
     ), 
     array( 
       "title" => "Learn HTML from m.hgci.cn", 
       "author" => "HTMLAuthor", 
       "pubYear" => 2002 
     ), 
     array( 
       "title" => "Learn CSS from m.hgci.cn", 
       "author" => "CSSAuthor", 
       "pubYear" => 2003 
     ), 
    ); 

    $bookNum = 0; 

    foreach ( $myBooks as $book ) { 
     $bookNum++; 
     echo "Book #$bookNum:"; 
     foreach ( $book as $key => $value ) { 
       echo "$key :$value \n"; 
     } 
    } 
 ?> 

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)