PHP array (array) Output three forms

Source: Internet
Author: User

 

Code

$ BBBB = Array ( " 11 " => " Aaa " , " 22 " => " Bbb " );

//Only output value and not output key
Foreach($ BBBB As $ Color){
Echo $ Color;
}

// Both value and key can be output.
Foreach ( $ BBBB   As   $ Key => $ Value )
{
Echo   $ Key . " => " . $ Value ;
}

// Both value and key can be output.
While ( $ Color = Each ( $ BBBB )){
Echo   $ Color [ ' Key ' ];
} Or While(List ($ key, $ value)=Each($ BBBB)){
Echo "$ Key: $ value <br>";
}

 

 

Directly access the array elements:

 

Code

<? PHP
$ Arr = Array ( ' W ' => ' Wen ' , ' J ' => ' Jian ' , ' B ' => ' Bao ' );

Echo ( $ Arr [ ' W ' ]) , ' <Br/> ' ;//Function
Echo ( $ Arr [W]) , ' <Br/> ' ;//Function
Echo ( $ Arr [ 0 ]) , ' <Br/> ' ; // No Why ???
Echo ( $ Arr [ ' J ' ]) , ' <Br/> ' ;//Function
Echo ( $ Arr [J]) , ' <Br/> ' ;//Function
Echo ( $ Arr [ 1 ]) , ' <Br/> ' ; // No Why ???
Echo ( $ Arr [ ' B ' ]) , ' <Br/> ' ;//Function
Echo ( $ Arr [B]) , ' <Br/> ' ;//Function
Echo ( $ Arr [ 2 ]) , ' <Br/> ' ; // No Why ???

?>

 

Output:

Wen
Wen

Jian
Jian

Bao
Bao
 

 


Question:

AccessJoin ArrayElement,
1. The "key" in [] can also be accessed without quotation marks ???
2. array index access does not work ???

 

<? PHP
$ Arr1 = Array ( ' Wen ' , ' Jian ' , ' Bao ' );

echo $ arr1 [ 0 ] , '
' , $ arr1 [ 1 ] , '
' , $ arr1 [ 2 ];
?>

 

Output:

 

Wen
Jian
Bao

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.