一些最重要的PHP数组函数

http://php.net/manual/zh/ref.array.php

http://www.w3school.com.cn/php/php_ref_array.asp

 

挑出一些最有用的函数:

array_combine() 通过合并两个数组来创建一个新数组。
array_intersect() 计算数组的交集
array_diff() 返回两个数组的差集数组
array_merge() 把一个或多个数组合并为一个数组
array_pop() 将数组最后一个单元弹出(出栈)。
array_push() 将一个或多个单元(元素)压入数组的末尾(入栈)。
array_search() 在数组中搜索给定的值,如果成功则返回相应的键名。
array_shift() 删除数组中的第一个元素,并返回被删除元素的值。
array_map() 将回调函数作用到给定数组的单元上。(很有意思)
array_slice() 切片函数,取出一部分数据
array_sum() 计算数组中所有值的和
array_unique() 删除数组中重复的值
array_walk() 对数组中的每个成员应用用户函数

in_array — 检查数组中是否存在某个值
array_keys — 返回数组中所有的键名
array_values — 返回数组中所有的值
array_key_exists() 检查给定的键名或索引是否存在于数组中

reset() 函数 current() 函数 next() 和 prev() 函数 end() 函数


遍历输出
$people = array("Peter", "Joe", "Glenn", "Cleveland");
reset($people);
while (list($key, $val) = each($people))
echo "$key => $val<br />";

// 第三种方法更新(整齐有序):
// 此角色拥有的所有模块的列表
$in_modules = substr($modules,0,strlen($modules)-1); 
// 一级模块的空数组
$modules111 = array();
// 根据二级模块,得出所有一级模块
$query = "select * from u_module where (module_parent<>0) and module_id in ($in_modules)";
echo $query."<br>qqq<br>";
$result=mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
    //echo "i=ssssss.<br>";
    array_push($modules111, $row["module_parent"]);
}
// 合并模块一级模块数组
$modules111 = array_unique($modules111);
//print_r($modules111)."<br>";
// 转换成一级模块字符串
$modules___="";
reset($modules111);
foreach ($modules111 as $i => $val) {
    //echo "i=$i.<br>";
    if ($modules___=="") $modules___.=$val; else $modules___.=",".$val;
}

 

需求:

查询以后,轻易得到一个数组,包括了所有相关部门。貌似push即可。

然后输出为用逗号分隔的字符串。

posted @ 2013-05-17 21:08  findumars  Views(304)  Comments(0Edit  收藏  举报