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

Methods for php array output

巴扎黑
Release: 2023-03-14 16:12:01
Original
1991 people have browsed it

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>";
}
Copy after login

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;;//不起作用,不知为什么???
?>
Copy after login


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];
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template