Home >Backend Development >PHP Tutorial >Methods for php array output

Methods for php array output

巴扎黑
巴扎黑Original
2017-08-04 16:38:511972browse

This article is a detailed analysis and introduction to the three forms of PHP array (array) output. For those who need it, please refer to

The code is as follows:

$bbbb=array("11"=>"aaa","22"=>"bbb");
//只能输出值value不能输出key
foreach($bbbb as $color)
{
  echo $color;
}
//value与key都可输出
foreach($bbbb as $key=>$value)
{
  echo $key."=>".$value;
}
//value与key都可输出
while($color=each($bbbb)){
  echo $color['key'];
}
或
while(list($key,$value)=each($bbbb)){
  echo "$key : $value<br>";
}

Direct access to array elements :

The code is as follows:

<?php
$arr=array(&#39;w&#39;=>&#39;wen&#39;,&#39;j&#39;=>&#39;jian&#39;,&#39;b&#39;=>&#39;bao&#39;);
echo($arr[&#39;w&#39;]),&#39;<br/>&#39;;//起作用
echo($arr[w]),&#39;<br/>&#39;;//起作用
echo($arr[0]),&#39;<br/>&#39;;//不起作用,不知为什么???
echo($arr[&#39;j&#39;]),&#39;<br/>&#39;;//起作用
echo($arr[j]),&#39;<br/>&#39;;//起作用
echo($arr[1]),&#39;<br/>&#39;;//不起作用,不知为什么???
echo($arr[&#39;b&#39;]),&#39;<br/>&#39;;//起作用
echo($arr[b]),&#39;<br/>&#39;;//起作用
echo($arr[2]),&#39;<br/>&#39;;//不起作用,不知为什么???
?>


Output:

The code is as follows:

wen
wen
jian
jian
bao
bao


Suspicious points:
Accessing associative array elements,
1. Can the "key" in [ ] be accessed without using quotation marks ("")? ? ?
 2. Array index access doesn’t work? ? ?

The code is as follows:

<?php
$arr1=array(&#39;wen&#39;,&#39;jian&#39;,&#39;bao&#39;);
echo $arr1[0],&#39;<br/>&#39;,$arr1[1],&#39;<br/>&#39;,$arr1[2];
?>

Output:

The code is as follows:

wen
jian
bao

The above is the detailed content of Methods for php array output. For more information, please follow other related articles on the PHP Chinese website!

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