🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 第二课 数组 > 声名数组两种写法 ``` <pre class="calibre14">``` $arr <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token3">1</span><span class="token2">,</span><span class="token3">2</span><span class="token2">,</span><span class="token3">3</span><span class="token2">)</span><span class="token2">;</span> $arr1<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span><span class="token">=</span><span class="token4">'a'</span><span class="token2">;</span> $arr1<span class="token2">[</span><span class="token3">2</span><span class="token2">]</span><span class="token">=</span><span class="token4">'b'</span><span class="token2">;</span> ``` ``` > 遍历数组 ``` <pre class="calibre14">``` $arr <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'a'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">,</span><span class="token4">'b'</span><span class="token">=</span><span class="token">></span><span class="token3">2</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">foreach</span><span class="token2">(</span>$arr as $k<span class="token">=</span><span class="token">></span>$v<span class="token2">)</span><span class="token2">{</span> echo $k<span class="token2">.</span><span class="token4">":"</span><span class="token2">.</span>$v<span class="token2">.</span><span class="token4">"\n"</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">while</span><span class="token2">(</span><span class="token1">list</span><span class="token2">(</span>$k<span class="token2">,</span>$v<span class="token2">)</span><span class="token">=</span><span class="token1">each</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> echo $k<span class="token2">.</span><span class="token4">":"</span><span class="token2">.</span>$v<span class="token2">.</span><span class="token4">"\n"</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` - \[x\]字符数组转换 ``` <pre class="calibre14">``` <span class="token1">explode</span><span class="token2">(</span><span class="token4">'-'</span><span class="token2">,</span><span class="token4">'1-2-3-4'</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//分割成数组</span> <span class="token1">implode</span><span class="token2">(</span><span class="token4">'-'</span><span class="token2">,</span>$arr<span class="token2">)</span><span class="token2">;</span><span class="token6">//转换成字符串</span> ``` ``` > 元素个数 count($arr) - \[\] 查找元素,返回下标 ``` <pre class="calibre14">``` $str <span class="token">=</span> <span class="token1">array_search</span><span class="token2">(</span><span class="token4">'1'</span><span class="token2">,</span><span class="token1">array</span><span class="token2">(</span><span class="token4">'k'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">,</span><span class="token4">'f'</span><span class="token">=</span><span class="token">></span><span class="token3">2</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> ``` ``` > 合并数组 `$result = array_merge($array1, $array2);` > 返回键名 ``` <pre class="calibre16">``` $arr <span class="token">=</span> <span class="token1">array_keys</span><span class="token2">(</span>$array<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> $str <span class="token">=</span> <span class="token1">array_pop</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">;</span>取出最后一个<span class="token2">,</span>返回的单个元素<span class="token2">,</span>原数组少一个 $str <span class="token">=</span> <span class="token1">array_shift</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">;</span> 取出第一个 <span class="token1">array_push</span><span class="token2">(</span>$arr<span class="token2">,</span> <span class="token4">"apple"</span><span class="token2">,</span> <span class="token4">"raspberry"</span><span class="token2">)</span><span class="token2">;</span> 压入末尾元素 $k <span class="token">=</span> <span class="token1">array_rand</span><span class="token2">(</span>$arr<span class="token2">,</span> <span class="token3">2</span><span class="token2">)</span> 随机取出<span class="token3">2</span>个元素<span class="token2">,</span>返回下标 $result <span class="token">=</span> <span class="token1">array_reverse</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">;</span> 反序 $arr <span class="token">=</span> <span class="token1">array_slice</span><span class="token2">(</span>$input<span class="token2">,</span> <span class="token">-</span><span class="token3">2</span><span class="token2">,</span> <span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> 从某个位置开始<span class="token2">,</span>取出元素个数<span class="token2">,</span>负数从末尾开始<span class="token2">,</span>不填写到末尾 <span class="token1">array_sum</span><span class="token2">(</span>$a<span class="token2">)</span>数值之和 <span class="token1">array_unique</span><span class="token2">(</span>$input<span class="token2">)</span> 去掉重复 <span class="token1">array_unshift</span><span class="token2">(</span>$queue<span class="token2">,</span> <span class="token4">"apple"</span><span class="token2">,</span> <span class="token4">"raspberry"</span><span class="token2">)</span><span class="token2">;</span>开头插入 $arr <span class="token">=</span> <span class="token1">array_values</span><span class="token2">(</span>$array<span class="token2">)</span> 返回值组成的数组 <span class="token1">count</span><span class="token2">(</span>$arr<span class="token2">)</span>计算数组个数 <span class="token1">in_array</span><span class="token2">(</span><span class="token4">"Irix"</span><span class="token2">,</span> $arr<span class="token2">)</span><span class="token2">)</span> 元素是否在数组中 <span class="token1">range</span><span class="token2">(</span><span class="token3">0</span><span class="token2">,</span> <span class="token3">100</span><span class="token2">,</span> <span class="token3">10</span><span class="token2">)</span> <span class="token3">0</span><span class="token">-</span><span class="token3">100</span> 步长<span class="token3">10</span>的数组 $k <span class="token">=</span> <span class="token1">range</span><span class="token2">(</span><span class="token4">'a'</span><span class="token2">,</span><span class="token4">'z'</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">shuffle</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">;</span> 打乱数组 ``` ```