Heim > Artikel > Backend-Entwicklung > PHP根本语法(四)
PHP基本语法(四)
数组是特殊的变量,它可以同时保存一个以上的值。
数组能够在单一变量名中存储许多值,并且您能够通过引用下标号来访问某个值。
<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 )
<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 )
<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
<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
<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
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原创文章,需要转载的请联系我,有问题欢迎评论或私信。