mysql 数组处理函数_数组相关处理函数
一、数组键值相关处理函数1. array_values() //返回输入的所有值,并建立索引1 $temp=array("os"=>"linux","webserver"=>"Apache", "db"=>"MySql", "laguage"=>"php");2 $arr=array_values($temp);3 echo '';4 print_r($arr);5 ec
一、数组键值相关处理函数
1. array_values() //返回输入的所有值,并建立索引
1 $temp=array("os"=>"linux","webserver"=>"Apache", "db"=>"MySql", "laguage"=>"php");2 $arr=array_values($temp);3 echo '
';4 print_r($arr);5 echo '';6
7 //result
8 Array
9 (10 [0] =>linux11 [1] =>Apache12 [2] => MySql
13 [3] =>php14 )
2. array_keys() //返回数组中所有的键名
array_keys(inputarray,[mixed value]) //若指定要搜索的值,则返回该值的键名,否则返回全部
1 $temp=array("os"=>"linux","webserver"=>"Apache", "db"=>"MySql", "laguage"=>"php");2 $arr=array_keys($temp);3 echo '
';4 print_r($arr);5 echo '';6
7 $arr=array_keys($temp, "MySql");8 echo '
';9 print_r($arr);10 echo '';11
12 //value
13 Array
14 (15 [0] =>os16 [1] =>webserver17 [2] =>db18 [3] =>laguage19 )20 Array
21 (22 [0] =>db23 )
3. bool in_array ( mixed needle, array haystack [, bool strict])
//检查数组中是否存在某个值
1 $temp=array("os"=>"linux","webserver"=>"Apache", "db"=>"MySql", "laguage"=>"php");2 if( in_array("Apache", $temp) ){3 echo 'find Apache!'.'
';4 }5
6 //result
7 find Apache!
4. bool array_key_exists ( mixed key, array search)
//检查给定的键名或者索引是否存在于数组中
$temp=array("os"=>"linux","webserver"=>"Apache", "db"=>"MySql", "language"=>"php");if( array_key_exists("language", $temp) ){echo 'language is in array!'.'
';
}//result
language is inarray!
5. array array_flip ( array trans)
//交换数组中的键和值
1 $temp=array("os"=>"linux","webserver"=>"Apache", "db"=>"MySql", "language"=>"php");2
3 echo '
';4 print_r($temp);5 echo '
';6
7 $temp=array_flip($temp);8 echo '
';9 print_r($temp);10 echo '
';11
12 //result
13 Array ( [os] => linux [webserver] => Apache [db] => MySql [language] =>php )14
15 Array ( [linux] => os [Apache] => webserver [MySql] => db [php] => language )
6. array array_reverse ( array array [, bool preserve_keys])
//返回一个单元顺序相反的数组
1 $temp=array("os"=>"linux","webserver"=>"Apache", "db"=>"MySql", "language"=>"php");2
3 $temp=array_reverse($temp);4 echo '
';5 print_r($temp);6 echo '';7
8 $temp=array_reverse($temp);9 echo '
';10 print_r($temp);11 echo '';12
13 //result
14 Array
15 (16 [language] =>php17 [db] => MySql
18 [webserver] =>Apache19 [os] =>linux20 )21 Array
22 (23 [os] =>linux24 [webserver] =>Apache25 [db] => MySql
26 [language] =>php27 )
二、统计数组元素的个数
1. array array_count_values ( array input)
//统计数组中所有值出现的次数,返回的依然是数组
1 $temp=array("os"=>"linux","webserver"=>"Apache", "db"=>"MySql", "language"=>"php");2
3 $count=array_count_values($temp);4
5 print_r($count);6
7
8 //result
9
10 Array ( [linux] => 1 [Apache] => 1 [MySql] => 1 [php] => 1 )
2. array array_unique ( array array)
//移除数组中重复的值
1 $temp=array("abc"=>"MySql", "os"=>"linux","webserver"=>"Apache", "db"=>"MySql", "language"=>"php");2
3 $count=array_unique($temp);4
5 print_r($count);6
7 //result
8
9 Array ( [abc] => MySql [os] => linux [webserver] => Apache [language] => php )
三、使用回调函数处理数组的函数
1. array array_filter ( array input [, callback function])
//回调函数过滤数组中的元素
array_filter() 依次将 input 数组中的每个值传递到 callback 函数。如果 callback 函数返回 TRUE,则 input 数组的当前值会被包含在返回的结果数组中。数组的键名保留不变。
1 function ji($var){2 return ($var % 2 == 1);3 }4 function ou($var){5 return ($var % 2 == 0);6 }7
8 $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);9 $array2 = array(6, 7, 8, 9, 10, 11, 12);10
11 echo "jishu: ";12 print_r(array_filter($array1, "ji"));13 echo "oushu: ";14 print_r(array_filter($array2, "ou"));15
16 //result
17 jishu: Array ( [a] => 1 [c] => 3 [e] => 5 ) oushu: Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12 )
2. bool array_walk ( array array, callback function [, mixed userdata])
//对数组中的每个成员应用用户函数,其中回调函数的第一个值为值,第二个为键,第三个为array_walk的第三个参数
1 function guoyuan($value,$key){2 echo $key.'--is--'.$value.'
';3 }4 function guoyuan1($value,$key,$class){5 echo $class.':'.$key."-is-".$value.'
';6 }7
8 $fruits=array("a"=>"apple", "b"=>"banana", "p"=>"pear", "o"=>"orange");9
10 array_walk($fruits, "guoyuan");11 array_walk($fruits, "guoyuan1","fruits");12
13 //result
14 a--is--apple15 b--is--banana16 p--is--pear17 o--is--orange18 fruits:a-is-apple19 fruits:b-is-banana20 fruits:p-is-pear21 fruits:o-is-orange
3. array array_map ( mixed callback, array arr1 [, array ...])
//将回调函数作用到给定数组的单元上,返回的也是一个数组
function cube($value){return $value*$value*$value;
}$num=array(1,2,3,4,5,6,7,8,9);$cube_num=array_map("cube",$num);echo '
';print_r($cube_num);echo '';//result
Array(
[0] => 1[1] => 8[2] => 27[3] => 64[4] => 125[5] => 216[6] => 343[7] => 512[8] => 729)
可以传入多个数组,回调函数中参数的个数应该跟传入数组的个数一致
function test($first_n, $sec_n){return $first_n.'--is--'.$sec_n;
}$num=array(1,2,3,4,5,6,7,8,9);$num_en=array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine");$result=array_map("test", $num, $num_en);echo '
';print_r($result);echo '';//result
Array(
[0] => 1--is--one
[1] => 2--is--two
[2] => 3--is--three
[3] => 4--is--four
[4] => 5--is--five
[5] => 6--is--six
[6] => 7--is--seven
[7] => 8--is--eight
[8] => 9--is--nine
)
可以通过NULL构造一个数组的数组不用回调函数
$num=array(1,2,3,4,5,6,7,8,9);$num_en=array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine");$num_2=array(11,22,33,44,55,66,77,88,99);$result=array_map(NULL, $num, $num_en, $num_2);echo '
';print_r($result);echo '';//result
Array(
[0] => Array(
[0] => 1[1] =>one
[2] => 11)
[1] => Array(
[0] => 2[1] =>two
[2] => 22)
[2] => Array(
[0] => 3[1] =>three
[2] => 33)
[3] => Array(
[0] => 4[1] =>four
[2] => 44)
[4] => Array(
[0] => 5[1] =>five
[2] => 55)
[5] => Array(
[0] => 6[1] =>six
[2] => 66)
[6] => Array(
[0] => 7[1] =>seven
[2] => 77)
[7] => Array(
[0] => 8[1] =>eight
[2] => 88)
[8] => Array(
[0] => 9[1] =>nine
[2] => 99)
)
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)