Home >Backend Development >PHP Tutorial >Summary of PHP array introductory tutorial_PHP tutorial

Summary of PHP array introductory tutorial_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:46:33903browse

这么多的数组函数我们该怎么学习?其实我们只需要清楚,我们需要对数组做哪些操作,然后把这些操作归类,心中就有了大概的印象,然后随着你的实践,这些函数你就很清楚了,在这里提醒大家的是手册要常备!废话不多说,数组函数一般归为以下几类(看到英文别怕哦):
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();
Similar to array_pop, except that it shifts out and returns the first value of the array. Numeric key names will start counting from zero again, while text key names will remain unchanged!

array_unshift();
Similar to array_push, except that it inserts data from the head of the array ! Numeric key names will start counting from zero again, while text key names will remain unchanged!

array_pad();
array array_pad ( array input, intpad_size, mixed pad_value )
Pall the array to the specified length with values!
If pad_size is positive, it will be filled from the right, if it is negative, it will be filled from the left. If it is less than the length of the array, it will keep the array unchanged! See the manual for examples!

Locating array elements
in_array();
Check Whether there is a certain value in the array, I won’t go into the basics, see the manual for details!

array_keys();
array array_keys ( input array [,mixed search_value [ , bool strict]] )
Returns all key names in the array. If search_value is selected, the selected key name is returned! Since PHP 5], you can use the strict parameter to perform congruent comparisons ( ===).

array_key_exists();
bool array_key_exists ( mixed key,array search )
Checks whether the given key name or index exists in the array, and returns true if it exists. Remember to check the manual for some more specific applications!

array_values();
array array_values ( input array )
Similar to array_keys()! What is returned is all the key values ​​​​of the array!

array_search();
mixed array_search ( mixed needle, array haystack [, bool strict] )
Search the haystack for the needle parameter and return the key name if found, otherwise return FALSE .
If needle is a string, the comparison is case-sensitive!
If strict is true, the type must be compared
If needle appears more than once in haystack, the first matching key is returned. To return all keys that match a value, array_keys() should be used instead with the optional parameter search_value!


Traversing arrays

key();

mixed key ( array &array )
Returns the key name of the current unit in the array!

reset();
Reset the array pointer.

each();
Returns the key of the current cell in the array /value pair and move the pointer down one position!

current();
Returns the value of the array cell currently pointed to by the internal pointer, and Does not move the pointer. If the end of the cell list is exceeded, false is returned.

end();
Move the internal pointer of array to The last cell and returns its value.

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();
Note that the difference from array_intersect() is that the key name is also used Compare.

array_diff();
is similar to array_intersect(), only However, it calculates the difference set of arrays

array_diff_assoc();
and array_intersect_assoc () is similar, except that it calculates the difference set of arrays

Other useful array functions

array_rand();
mixed array_rand ( array input [,int num_req] )
Randomly take one or more cells from the array.

shuffle();
bool shuffle ( array &array )
This function scrambles (randomly arranges the order of cells) an array.
This function assigns new key names to the cells in array. This will delete the original keys rather than just reorder them.

array_sum();
Convert the sum of all values ​​in the array to an integer or floating point number The result is returned.

array_chunk();
array array_chunk ( input array, intsize [, bool preserve_keys] )
Split an array into multiple arrays, where the number of cells in each array is determined by size. The last array may have a few fewer elements. The resulting array is a cell in a multidimensional array, with indexes starting from zero.
Set the optional parameter preserve_keys to TRUE to enable PHP to retain the original key names in the input array. If you specify FALSE, each result array will be indexed with a new number starting from zero. The default value is FALSE

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320108.htmlTechArticleHow should we learn so many array functions? In fact, we just need to know what operations we need to do on the array, and then classify these operations, so that we have a general impression in our mind, and then...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn