PHP根本语法(四)

WBOY
发布: 2016-06-13 12:23:16
原创
800 人浏览过

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

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板