Heim > Backend-Entwicklung > PHP-Tutorial > PHP根本语法(四)

PHP根本语法(四)

WBOY
Freigeben: 2016-06-13 12:23:16
Original
819 Leute haben es durchsucht

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>
Nach dem Login kopieren

<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>
Nach dem Login kopieren

运行结果: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>
Nach dem Login kopieren

<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>
Nach dem Login kopieren

运行结果: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>
Nach dem Login kopieren

运行结果:
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>
Nach dem Login kopieren

运行结果:
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>
Nach dem Login kopieren

运行结果:
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>
Nach dem Login kopieren

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

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage