>  기사  >  백엔드 개발  >  PHP根本语法(四)

PHP根本语法(四)

WBOY
WBOY원래의
2016-06-13 12:23:16795검색

PHP基本语法(四)

十一、数组

1、什么是数组?

数组是特殊的变量,它可以同时保存一个以上的值。
数组能够在单一变量名中存储许多值,并且您能够通过引用下标号来访问某个值。

2、三种数组类型

  • 索引数组 - 带有数字索引的数组
  • 关联数组 - 带有指定键的数组
  • 多维数组 - 包含一个或多个数组的数组

3、用array()创建数组

(1)、创建索引数组

<code class=" hljs php"><span class="hljs-variable">$its</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">"Google"</span>,<span class="hljs-string">"Apple"</span>,<span class="hljs-string">"Microsoft"</span>);print_r(<span class="hljs-variable">$its</span>);</code>

<code class=" hljs bash"><span class="hljs-variable">$its</span>[<span class="hljs-number">0</span>]=<span class="hljs-string">"Google"</span>;<span class="hljs-variable">$its</span>[<span class="hljs-number">1</span>]=<span class="hljs-string">"Apple"</span>;<span class="hljs-variable">$its</span>[<span class="hljs-number">2</span>]=<span class="hljs-string">"Microsoft"</span>;print_r(<span class="hljs-variable">$its</span>);</code>

运行结果:Array ( [0] => Google [1] => Apple [2] => Microsoft )

(2)、创建关联数组

<code class=" hljs php"><span class="hljs-variable">$age</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">"Peter"</span>=><span class="hljs-string">"20"</span>,<span class="hljs-string">"Bill"</span>=><span class="hljs-string">"19"</span>,<span class="hljs-string">"Ken"</span>=><span class="hljs-string">"21"</span>);print_r(<span class="hljs-variable">$age</span>);</code>

<code class=" hljs bash"><span class="hljs-variable">$age</span>[<span class="hljs-string">"Peter"</span>]=<span class="hljs-string">"20"</span>;<span class="hljs-variable">$age</span>[<span class="hljs-string">'Bill'</span>]=<span class="hljs-string">"19"</span>;<span class="hljs-variable">$age</span>[<span class="hljs-string">"Ken"</span>]=<span class="hljs-string">"21"</span>;print_r(<span class="hljs-variable">$age</span>);</code>

运行结果:Array ( [Peter] => 20 [Bill] => 19 [Ken] => 21 )

4、用count()获取数组长度

<code class=" hljs php"><span class="hljs-variable">$its</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">"Google"</span>,<span class="hljs-string">"Apple"</span>,<span class="hljs-string">"Microsoft"</span>);<span class="hljs-variable">$age</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">"Peter"</span>=><span class="hljs-string">"20"</span>,<span class="hljs-string">"Bill"</span>=><span class="hljs-string">"19"</span>,<span class="hljs-string">"Ken"</span>=><span class="hljs-string">"21"</span>);<span class="hljs-keyword">echo</span> count(<span class="hljs-variable">$its</span>).<span class="hljs-string">"<br />"</span>;<span class="hljs-keyword">echo</span> count(<span class="hljs-variable">$age</span>);</code>

运行结果:
3 <br>3

5、用for和foreach遍历数组

(1)、用for遍历索引数组

<code class=" hljs php"><span class="hljs-variable">$its</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">"Google"</span>,<span class="hljs-string">"Apple"</span>,<span class="hljs-string">"Microsoft"</span>);<span class="hljs-keyword">for</span>(<span class="hljs-variable">$i</span>=<span class="hljs-number">0</span>;<span class="hljs-variable">$i</span><count(<span class="hljs-variable">$its</span>);<span class="hljs-variable">$i</span>++){    <span class="hljs-keyword">echo</span> <span class="hljs-variable">$its</span>[<span class="hljs-variable">$i</span>].<span class="hljs-string">"<br />"</span>;}</code>

运行结果:
Google <br>Apple <br>Microsoft

(2)、用foreach遍历关联数组

<code class=" hljs php"><span class="hljs-variable">$age</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">"Peter"</span>=><span class="hljs-string">"20"</span>,<span class="hljs-string">"Bill"</span>=><span class="hljs-string">"19"</span>,<span class="hljs-string">"Ken"</span>=><span class="hljs-string">"21"</span>);<span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$age</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$value</span>){<span class="hljs-keyword">echo</span> <span class="hljs-variable">$value</span>.<span class="hljs-string">"<>"</span>;}</code>

运行结果:
20 <br>19 <br>21

6、数组排序

sort() - 以升序对数组排序
rsort() - 以降序对数组排序
asort() - 根据值,以升序对关联数组进行排序
ksort() - 根据键,以升序对关联数组进行排序
arsort() - 根据值,以降序对关联数组进行排序
krsort() - 根据键,以降序对关联数组进行排序

如何记住它们?

r—-reduce,减少
a—-value,值(不知道是不是,暂且这样记)
k—-key,键

<code class=" hljs php"><span class="hljs-variable">$age</span>=<span class="hljs-keyword">array</span>(<span class="hljs-string">"Peter"</span>=><span class="hljs-string">"20"</span>,<span class="hljs-string">"Bill"</span>=><span class="hljs-string">"19"</span>,<span class="hljs-string">"Ken"</span>=><span class="hljs-string">"21"</span>);<span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$age</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$value</span>){<span class="hljs-keyword">echo</span> <span class="hljs-variable">$value</span>.<span class="hljs-string">"<br />"</span>;}arsort(<span class="hljs-variable">$age</span>);<span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$age</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$value</span>){<span class="hljs-keyword">echo</span> <span class="hljs-variable">$value</span>.<span class="hljs-string">"<br />"</span>;}</code>

运行结果:
20 <br>19 <br>21
21
20
19

版权声明:本文为Lshare原创文章,需要转载的请联系我,有问题欢迎评论或私信。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:php一道走来다음 기사:生手搭建ThinkPHP框架