Table des matières
PHP代码:
Maison php教程 php手册 PHP 数组入门教程小结

PHP 数组入门教程小结

Jun 13, 2016 pm 12:23 PM
php Faire 入门教程 函数 Lequel 学习 怎么 nous 数组 besoin

这么多的数组函数我们该怎么学习?其实我们只需要清楚,我们需要对数组做哪些操作,然后把这些操作归类,心中就有了大概的印象,然后随着你的实践,这些函数你就很清楚了,在这里提醒大家的是手册要常备!废话不多说,数组函数一般归为以下几类(看到英文别怕哦):
Outputting arrays
Creating arrays
Testing for an array
Locating array elements
Traversing arrays
Determining array size and element uniqueness
Sorting arrays
Merging, slicing, splicing, and dissecting arrays


Outputting arrays
print_r()
不熟悉这个的我就不想多说了!最基本的函数,当然要输出数组也可是使用循环结构输出,不过有这么方便的输出函数干吗不用呢!

Creating arrays
array()
这个函数我也不想说了,这其实只能算语言结构,大家在新建数组的时候都用到过,没用过的我只能说,兄弟,我无语了!
list()
这个函数和上面提到的 array()一样,只是个语言结构,它通过一步操作给一组变量赋值!
具体示例请查看手册!
range();
array range ( mixed low, mixed high[, number step] )
建立一个包含指定范围单元的数组
示例: $arr =range(0, 6);
相当于 $arr =array(0, 1, 2, 3, 4, 5, 6);
一般数据按从低到高排列,如果 low> high,则从高到低排列;

Testing for an array
is_array();
测试变量是否array类型,是则返回true,不是则返回false,很基本,很实用的函数!

Adding and removing array elements
array_push()
int array_push ( array &target_array,mixed var [, mixed ...] )
将一个或多个单元从末尾添加至数组!返回新数组的单元总数!
示例:

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$states <span style="COLOR: #007700">=array(</span> <span style="COLOR: #0000bb">‘Ohio'</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">‘New York'</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">array_push</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$states</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">‘California'</span> <span style="COLOR: #007700">,</span> <span style="COLOR: #0000bb">‘Texas'</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #ff8000">//output: array((‘Ohio',‘New York' , ‘California',‘Texas');</span> <br><br></span>

array_pop();
弹出并返回数组的最后一个单元,并将数组长度减一。使用后会重置数组指针!
示例:

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$states <span style="COLOR: #007700">=array(</span> <span style="COLOR: #0000bb">‘Ohio'</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">‘New York'</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">$state </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">array_pop</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$states</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #ff8000">//output: New York</span> <br><br></span>

array_shift();
与array_pop类似,只不过它移出和返回的是数组的第一个值。数字键名将重新从零开始记数,文字键名则保持不变!

array_unshift();
与array_push类似,只不过是从数组头部插入数据!数字键名将重新从零开始记数,文字键名则保持不变!

array_pad();
array array_pad ( array input, intpad_size, mixed pad_value )
用值将数组填充到指定长度!
pad_size为正,则从右侧开始填补,为负,从左侧开始填补,小与数组长度,则保持数组不变!示例见手册!

Locating array elements
in_array();
检查数组中是否存在某个值,基础,不说了,具体见手册!

array_keys();
array array_keys ( array input [,mixed search_value [, bool strict]] )
返回数组中的所有键名。如果选定search_value,则返回选定的键名!自PHP 5]起,可以用strict参数来进行全等比较( ===)。

array_key_exists();
bool array_key_exists ( mixed key,array search )
检查给定的键名或索引是否存在于数组中,存在则返回true。一些更具体的应用记得查看手册!

array_values();
array array_values ( array input )
与array_keys()类似!返回的是数组的所有键值!

array_search();
mixed array_search ( mixed needle,array haystack [, bool strict] )
在 haystack中搜索 needle参数并在找到的情况下返回键名,否则返回 FALSE
如果needle是字符串,则比较区分大小写!
如果strict是true,还得比较类型
如果needle在haystack中出现不止一次,则返回第一个匹配的键。要返回所有匹配值的键,应该用array_keys()加上可选参数search_value来代替!


Traversing arrays

key();

mixed key ( array &array )
返回数组中当前单元的键名!

reset();
重置数组的指针。

each();
返回数组中当前单元的键 /值对,并将指针下移一位!

current();
返回当前被内部指针指向的数组单元的值,并不移动指针。如果超出了单元列表的末端,则返回 false

end();
array的内部指针移动到最后一个单元并返回其值。

next();
返回数组内部指针指向的下一个单元的值,或当没有更多单元时返回 FALSE

prev();
返回数组内部指针指向的前一个单元的值,或当没有更多单元时返回 FALSE

array_walk();
bool array_walk ( array &array,callback funcname [, mixed userdata] )
具体描述请参考手册!
示例:

array_reverse();
array array_reverse ( array array [,bool preserve_keys] )
接受数组 array作为输入并返回一个单元为相反顺序的新数组,如果 preserve_keys TRUE 则保留原来的键名。
示例:

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$states <span style="COLOR: #007700">=array(</span> <span style="COLOR: #0000bb">‘Delaware'</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">‘Pennsylvania'</span> <span style="COLOR: #007700">,</span> <span style="COLOR: #0000bb">‘New Jersey'</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">print_r</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">array_reverse</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$states</span> </span><span style="COLOR: #007700"><span style="FONT-FAMILY: 新宋体"><font color="#007700">));<br></font></span></span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #ff8000">//Array ([0]=>New Jersey [1]=>Pennsylvania[2]=>Delaware)<br></span><span style="COLOR: #0000bb">$states </span><span style="COLOR: #007700">= array(</span> <span style="COLOR: #0000bb">‘Delaware'</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">‘Pennsylvania'</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">‘New   Jersey'</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">print_r</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">array_reverse</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$states</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">1</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">));<br></span><span style="COLOR: #ff8000">//Array ([2]=>New Jersey [1]=>Pennsylvania[0]=>Delaware)</span> <br><br></span>

array_flip();
array array_flip ( array trans )
交换数组中的键 /值对,注意 trans 中的值需要能够作为合法的键名,例如需要是 integer 或者 string 。如果值的类型不对将发出一个警告,并且有问题的键/值对将不会反转。
如果同一个值出现了多次,则最后一个键名将作为它的值,所有其它的都丢失了。
示例:

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$trans <span style="COLOR: #007700">=array(</span> <span style="COLOR: #dd0000">"a" </span><span style="COLOR: #007700">=> </span><span style="COLOR: #0000bb">1</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"b" </span><span style="COLOR: #007700">=> </span><span style="COLOR: #0000bb">1</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"c" </span><span style="COLOR: #007700">=> </span><span style="COLOR: #0000bb">2</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">$trans </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">array_flip</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$trans</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">print_r</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$trans</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #ff8000">//Array([1]=>b[2]=>c );</span> <br><br></span>

Determining array size and element uniqueness

count();
计算数组中的单元数目或对象中的属性个数,很基本哦!

array_count_values();
返回一个数组,该数组用input数组中的值作为键名,该值在input数组中出现的次数作为值。
示例:

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$array<span style="COLOR: #007700">= array(</span> <span style="COLOR: #0000bb">1</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"hello"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">1</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"world"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"hello"</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">print_r</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">array_count_values </span><span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$array</span> <span style="COLOR: #007700">));</span> <br><br></span>

array_unique();
array array_unique ( array array )
接受 array作为输入并返回没有重复值的新数组。
示例:

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$input <span style="COLOR: #007700">=array(</span> <span style="COLOR: #dd0000">"a" </span><span style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">"green"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"red"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"b"</span> <span style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">"green"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"blue"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"red"</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">$result </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">array_unique</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$input</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">print_r</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$result</span> <span style="COLOR: #007700">);</span> <br><br></span>

详细用法请参见手册!

Sorting arrays

sort();
本函数对数组进行排序。当本函数结束时数组单元将被从最低到最高重新安排。
注意 : 本函数为array中的单元赋予新的键名。这将删除原有的键名而不仅是重新排序。

natsort();
利用自然排序法对数组进行排序!
示例:

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$array1 <span style="COLOR: #007700">=</span> <span style="COLOR: #0000bb">$array2 </span><span style="COLOR: #007700">= array(</span> <span style="COLOR: #dd0000">"img12.png"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"img10.png"</span> <span style="COLOR: #007700">,</span> <span style="COLOR: #dd0000">"img2.png"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"img1.png"</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">sort</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$array1</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br>echo </span><span style="COLOR: #dd0000">"Standard sorting\n"</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">;<br></span><span style="COLOR: #0000bb">print_r</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$array1</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br><br></span><span style="COLOR: #0000bb">natsort</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$array2</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br>echo </span><span style="COLOR: #dd0000">"\nNatural order sorting\n"</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">;<br></span><span style="COLOR: #0000bb">print_r</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$array2</span> <span style="COLOR: #007700">);</span> <br><br></span>

natcasesort();
natsort()一致,除了不区分大小写!

rsort();
sort()一致,这个是逆向排序。
asort();
sort()一直,但保持键名不变。
array_multisort();
对多个数组或多维数组进行排序,具体参考手册,有详细说明!
arsort();
rsort()类似,但保持键名不变。
ksort();
sort()类似,对键名进行排序。
krsort();
ksort()类似,但是逆向排序。
usort();
利用用户自定义函数对数组进行排序!

Merging, slicing, splicing, and dissectingarrays

array_combine();
array array_combine ( array keys,array values )
创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值

array_merge();
array array_merge ( array array1 [,array array2 [, array ...]] )
将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。
然而,如果数组包含数字键名,后面的值将不会 覆盖原来的值,而是附加到后面。
示例:

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$array1 <span style="COLOR: #007700">=array(</span> <span style="COLOR: #dd0000">"color" </span><span style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">"red"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">2</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">4</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">$array2 </span><span style="COLOR: #007700">= array(</span> <span style="COLOR: #dd0000">"a"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"b"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"color" </span><span style="COLOR: #007700">=></span> <span style="COLOR: #dd0000">"green"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"shape" </span><span style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">"trapezoid"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">4</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">$result </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">array_merge</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$array1</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">$array2</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">print_r</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$result</span> <span style="COLOR: #007700">);</span> <br><br></span>

如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$array1 </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">= array();<br></span><span style="COLOR: #0000bb">$array2 </span><span style="COLOR: #007700">= array(</span> <span style="COLOR: #0000bb">1 </span><span style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">"data"</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">$result </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">array_merge</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$array1</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">$array2</span> <span style="COLOR: #007700">);</span> <br><br></span>

如果你想完全保留原有数组并只想新的数组附加到后面,用+运算符:

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$array1 </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">= array();<br></span><span style="COLOR: #0000bb">$array2 </span><span style="COLOR: #007700">= array(</span> <span style="COLOR: #0000bb">1 </span><span style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">"data"</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">$result </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">$array1 </span><span style="COLOR: #007700">+ </span><span style="COLOR: #0000bb">$array2</span> <span style="COLOR: #007700">;</span> <br><br></span>

array_merge_recursive();
array array_merge_recursive ( arrayarray1 [, array ...] )
将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
如果输入的数组中有相同的字符串键名,则这些值会被合并到一个数组中去,这将递归下去,因此如果一个值本身是一个数组,本函数将按照相应的条目把它合并为另一个数组。然而,如果数组具有相同的数组键名,后一个值将不会覆盖原来的值,而是附加到后面。

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$ar1 <span style="COLOR: #007700">= array(</span> <span style="COLOR: #dd0000">"color" </span><span style="COLOR: #007700">=>array(</span> <span style="COLOR: #dd0000">"favorite" </span><span style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">"red"</span> <span style="COLOR: #007700">), </span><span style="COLOR: #0000bb">5</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">$ar2 </span><span style="COLOR: #007700">= array(</span> <span style="COLOR: #0000bb">10</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"color" </span><span style="COLOR: #007700">=> array(</span> <span style="COLOR: #dd0000">"favorite" </span><span style="COLOR: #007700">=></span> <span style="COLOR: #dd0000">"green"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"blue"</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">));<br></span><span style="COLOR: #0000bb">$result </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">array_merge_recursive</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$ar1</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">$ar2</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">print_r</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$result</span> <span style="COLOR: #007700">);</span> <br><br></span>

更详细的使用请参加手册!

array_slice();
array array_slice ( array array, intoffset [, int length [, bool preserve_keys]] )
从数组中取出一段。
如果offset非负,则序列将从array中的此偏移量开始。如果offset为负,则序列将从array中距离末端这么远的地方开始。
如果给出了length并且为正,则序列中将具有这么多的单元。如果给出了length并且为负,则序列将终止在距离数组末端这么远的地方。如果省略,则序列将从offset开始一直到array的末端。
示例:

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$input <span style="COLOR: #007700">= array(</span> <span style="COLOR: #dd0000">"a"</span> <span style="COLOR: #007700">,</span> <span style="COLOR: #dd0000">"b"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"c"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"d"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"e"</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">$output </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">array_slice</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$input</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">2</span> <span style="COLOR: #007700">);      </span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #ff8000">// returns"c", "d", and "e"<br></span><span style="COLOR: #0000bb">$output </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">array_slice</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$input</span> <span style="COLOR: #007700">, -</span> <span style="COLOR: #0000bb">2</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">1</span> <span style="COLOR: #007700">);  </span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #ff8000">// returns "d"<br></span><span style="COLOR: #0000bb">$output </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">array_slice</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$input</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">0</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">3</span> <span style="COLOR: #007700">);   </span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #ff8000">// returns "a","b", and "c"<br>// note the differences in the array keys<br></span><span style="COLOR: #0000bb">print_r</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">array_slice</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$input</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">2</span> <span style="COLOR: #007700">, -</span> <span style="COLOR: #0000bb">1</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">));<br></span><span style="COLOR: #0000bb">print_r</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">array_slice</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$input</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">2</span> <span style="COLOR: #007700">, -</span> <span style="COLOR: #0000bb">1</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">true</span> <span style="COLOR: #007700">));</span> <br><br></span>

array_splice();
array array_splice ( array&input, int offset [, int length [, array replacement]] )
把input数组中由offset和length指定的单元去掉,如果提供了replacement参数,则用replacement数组中的单元取代。返回一个包含有被移除单元的数组。注意input中的数字键名不被保留。
如果offset非负,则序列将从array中的此偏移量开始。如果offset为负,则序列将从array中距离末端这么远的地方开始。
如果给出了length并且为正,则序列中将具有这么多的单元。如果给出了length并且为负,则序列将终止在距离数组末端这么远的地方。如果省略,则序列将从offset开始一直到array的末端。
具体应用参见手册!

array_intersect();
array array_intersect ( arrayarray1, array array2 [, array ...] )
返回一个数组,该数组包含了所有在 array1 中也同时出现在所有其它参数数组中的值。注意键名保留不变。
示例:

复制PHP内容到剪贴板
PHP代码:
<span style="FONT-FAMILY: 新宋体">$array1 <span style="COLOR: #007700">=array(</span> <span style="COLOR: #dd0000">"a" </span><span style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">"green"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"red"</span> <span style="COLOR: #007700">,</span> <span style="COLOR: #dd0000">"blue"</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">$array2 </span><span style="COLOR: #007700">= array(</span> <span style="COLOR: #dd0000">"b" </span><span style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">"green"</span> <span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">"yellow"</span> <span style="COLOR: #007700">,</span> <span style="COLOR: #dd0000">"red"</span> </span><span style="FONT-FAMILY: 新宋体"><span style="COLOR: #007700">);<br></span><span style="COLOR: #0000bb">$result </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">array_intersect</span> <span style="COLOR: #007700">(</span> <span style="COLOR: #0000bb">$array1</span> <span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">$array2</span> <span style="COLOR: #007700">);</span> <br><br></span>

array_intersect_assoc();
注意和 array_intersect()不同的是键名也用于比较。

array_diff();
array_intersect()类似,只不过它是计算数组的差集

array_diff_assoc();
array_intersect_assoc()类似,只不过它是计算数组的差集

Other useful array functions

array_rand();
mixed array_rand ( array input [,int num_req] )
从数组中随机取出一个或多个单元。

shuffle();
bool shuffle ( array &array )
本函数打乱(随机排列单元的顺序)一个数组。
本函数为 array 中的单元赋予新的键名。这将删除原有的键名而不仅是重新排序。

array_sum();
将数组中的所有值的和以整数或浮点数的结果返回。

array_chunk();
array array_chunk ( array input, intsize [, bool preserve_keys] )
将一个数组分割成多个数组,其中每个数组的单元数目由 size 决定。最后一个数组的单元数目可能会少几个。得到的数组是一个多维数组中的单元,其索引从零开始。
将可选参数preserve_keys 设为 TRUE,可以使 PHP 保留输入数组中原来的键名。如果你指定了 FALSE,那每个结果数组将用从零开始的新数字索引。默认值是 FALSE

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques mois By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Comment réparer l'audio si vous n'entendez personne
1 Il y a quelques mois By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Commandes de chat et comment les utiliser
1 Il y a quelques mois By 尊渡假赌尊渡假赌尊渡假赌

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 apporte plusieurs nouvelles fonctionnalités, améliorations de sécurité et de performances avec une bonne quantité de dépréciations et de suppressions de fonctionnalités. Ce guide explique comment installer PHP 8.4 ou mettre à niveau vers PHP 8.4 sur Ubuntu, Debian ou leurs dérivés. Bien qu'il soit possible de compiler PHP à partir des sources, son installation à partir d'un référentiel APT comme expliqué ci-dessous est souvent plus rapide et plus sécurisée car ces référentiels fourniront les dernières corrections de bogues et mises à jour de sécurité à l'avenir.

Comment configurer Visual Studio Code (VS Code) pour le développement PHP Comment configurer Visual Studio Code (VS Code) pour le développement PHP Dec 20, 2024 am 11:31 AM

Visual Studio Code, également connu sous le nom de VS Code, est un éditeur de code source gratuit – ou environnement de développement intégré (IDE) – disponible pour tous les principaux systèmes d'exploitation. Avec une large collection d'extensions pour de nombreux langages de programmation, VS Code peut être c

7 fonctions PHP que je regrette de ne pas connaître auparavant 7 fonctions PHP que je regrette de ne pas connaître auparavant Nov 13, 2024 am 09:42 AM

Si vous êtes un développeur PHP expérimenté, vous aurez peut-être le sentiment d'y être déjà allé et de l'avoir déjà fait. Vous avez développé un nombre important d'applications, débogué des millions de lignes de code et peaufiné de nombreux scripts pour réaliser des opérations.

Comment analysez-vous et traitez-vous HTML / XML dans PHP? Comment analysez-vous et traitez-vous HTML / XML dans PHP? Feb 07, 2025 am 11:57 AM

Ce tutoriel montre comment traiter efficacement les documents XML à l'aide de PHP. XML (Language de balisage extensible) est un langage de balisage basé sur le texte polyvalent conçu à la fois pour la lisibilité humaine et l'analyse de la machine. Il est couramment utilisé pour le stockage de données et

Expliquez les jetons Web JSON (JWT) et leur cas d'utilisation dans les API PHP. Expliquez les jetons Web JSON (JWT) et leur cas d'utilisation dans les API PHP. Apr 05, 2025 am 12:04 AM

JWT est une norme ouverte basée sur JSON, utilisée pour transmettre en toute sécurité des informations entre les parties, principalement pour l'authentification de l'identité et l'échange d'informations. 1. JWT se compose de trois parties: en-tête, charge utile et signature. 2. Le principe de travail de JWT comprend trois étapes: la génération de JWT, la vérification de la charge utile JWT et l'analyse. 3. Lorsque vous utilisez JWT pour l'authentification en PHP, JWT peut être généré et vérifié, et les informations sur le rôle et l'autorisation des utilisateurs peuvent être incluses dans l'utilisation avancée. 4. Les erreurs courantes incluent une défaillance de vérification de signature, l'expiration des jetons et la charge utile surdimensionnée. Les compétences de débogage incluent l'utilisation des outils de débogage et de l'exploitation forestière. 5. L'optimisation des performances et les meilleures pratiques incluent l'utilisation des algorithmes de signature appropriés, la définition des périodes de validité raisonnablement,

Programme PHP pour compter les voyelles dans une chaîne Programme PHP pour compter les voyelles dans une chaîne Feb 07, 2025 pm 12:12 PM

Une chaîne est une séquence de caractères, y compris des lettres, des nombres et des symboles. Ce tutoriel apprendra à calculer le nombre de voyelles dans une chaîne donnée en PHP en utilisant différentes méthodes. Les voyelles en anglais sont a, e, i, o, u, et elles peuvent être en majuscules ou en minuscules. Qu'est-ce qu'une voyelle? Les voyelles sont des caractères alphabétiques qui représentent une prononciation spécifique. Il y a cinq voyelles en anglais, y compris les majuscules et les minuscules: a, e, i, o, u Exemple 1 Entrée: String = "TutorialSpoint" Sortie: 6 expliquer Les voyelles dans la chaîne "TutorialSpoint" sont u, o, i, a, o, i. Il y a 6 yuans au total

Expliquez la liaison statique tardive en PHP (statique: :). Expliquez la liaison statique tardive en PHP (statique: :). Apr 03, 2025 am 12:04 AM

Liaison statique (statique: :) ​​implémente la liaison statique tardive (LSB) dans PHP, permettant à des classes d'appel d'être référencées dans des contextes statiques plutôt que de définir des classes. 1) Le processus d'analyse est effectué au moment de l'exécution, 2) Recherchez la classe d'appel dans la relation de succession, 3) il peut apporter des frais généraux de performance.

Quelles sont les méthodes PHP Magic (__construct, __ destruct, __ call, __get, __set, etc.) et fournir des cas d'utilisation? Quelles sont les méthodes PHP Magic (__construct, __ destruct, __ call, __get, __set, etc.) et fournir des cas d'utilisation? Apr 03, 2025 am 12:03 AM

Quelles sont les méthodes magiques de PHP? Les méthodes magiques de PHP incluent: 1. \ _ \ _ Construct, utilisé pour initialiser les objets; 2. \ _ \ _ Destruct, utilisé pour nettoyer les ressources; 3. \ _ \ _ Appel, gérer les appels de méthode inexistants; 4. \ _ \ _ GET, Implémentez l'accès à l'attribut dynamique; 5. \ _ \ _ SET, Implémentez les paramètres d'attribut dynamique. Ces méthodes sont automatiquement appelées dans certaines situations, améliorant la flexibilité et l'efficacité du code.

See all articles