php常用数组处理方法

1. array_shift()  将数组第一个单元移出数据并返回。

demo1:

<?php 

$tmparray=array("1","2","3","4");

$tmparray=array_shift($tmparray);

print_r($tmparray);//array("1");

?>

2.array_chunk()将一个数组分割成多个数组,后面参数控制单元数目

<?php

$tmparray=array("a","b","c","d","e");

$tmparray=array_chunk($tmparray,2);

print_r($tmparray);//array(0=>array(0=>a,1=>b),1=>array(0=>c,1=>d),2=>array(0=>2));

?>

3.array_push()将传入的值压入array的末尾。

$tmparray=array("a","b");

$tmparray=array_push($tmparray,"c","d");

print_r($tmparray);//array(0=>a,1=>b,2=>c,3=>d);

?>

4.array_unshift() 将传入的单元整体插入到数组中 

<?php 
$tmparray= array ("a", "b"); 
$resarray = array_unshift ($tmparray, "c", "d"); 
print_r($resarray )//Array([0] => a[1] =>b[2] =>c[3] =>d) //返回4 数组个数
?> 

5.array_unique对数组去重,并返回一个新的数组 

<? 
$tmparray = ("a" => "a","b" => "b","c" => "c","d" => "b"); 
$resarray = array_unique($tmparray);//("a" => "a","b" => "b","c" => "c"); 
?> 

posted @ 2015-06-24 16:33  上善若水blog  阅读(202)  评论(0编辑  收藏  举报