Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP根本语法(四)

PHP根本语法(四)

WBOY
WBOYasal
2016-06-13 12:23:16795semak imbas

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原创文章,需要转载的请联系我,有问题欢迎评论或私信。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php一道走来Artikel seterusnya:生手搭建ThinkPHP框架