array('22',33,44,55,6)
in_array('22',$array)
//这样就会有一个把‘22’转换为22的操作,再进行比较
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true
$test = 0 ;
$array = array('a', 'b', 'c');
if (in_array($test, $array, true)) {
echo 'in array';
} else {
echo 'no';
}
null 或 string | string | 将 NULL 转换为 "",进行数字或词汇比较 |
bool 或 null | 任何其它类型 | 转换为 bool,FALSE < TRUE |
object | object | 内置类可以定义自己的比较,不同类不能比较,相同类和数组同样方式比较属性(PHP 4 中),PHP 5 有其自己的说明 |
string,resource 或 number | string,resource 或 number | 将字符串和资源转换成数字,按普通数学比较 |
array | array | 具有较少成员的数组较小,如果运算数 1 中的键不存在于运算数 2 中则数组无法比较,否则挨个值比较(见下例) |
array | 任何其它类型 | array 总是更大 |
object | 任何其它类型 | object 总是更大 |
array_key_exists 和 in_array 查询的东西都不一样吧
array_key_exists 判断是否有键值
array_key_exists(a,arr)->if(isset(arr[a]))就是true
而in_array 需要去遍历值 遍历到了才跳出循环
三种方式的语法区别
所以,从准确性的角度来看,array_key_exists是最准确的!
三种方式的使用建议
(鉴于empty与isset性能类似,但是isset准确性较高,这里就只比较isset与array_key_exists)
public static function inArray($item, $array) {
$flipArray = array_flip($array);
return isset($flipArray[$item]);
}
2、 用implode连接,直接用strpos判断
用implode函数+逗号连起来,直接用strpos判断。php里面字符串取位置速度非常快,尤其是在大数据量的情况下。不过需要注意的是首尾都要加"," ,这样比较严谨。如: ,user1,user2,user3, 查找的时候,查,user1,。还有strpos要用!== false,因为第一个会返回0。示例如下:
public static function inArray($item, $array) {
$str = implode(',', $array);
$str = ',' . $str . ',';
$item = ',' . $item . ',';
return false !== strpos($item, $str) ? true : false;
}
3、in_array 加第三个参数。
php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);
?>
经过此操作后,$stack 将只有 3 个单元:
Array
(
[0] => orange
[1] => banana
[2] => apple
)
并且 rasberry 将被赋给 $fruit。$fruit = "raspberry";
php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);
?>
php
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
?>
Array
(
[0] => apple
[1] => raspberry
[2] => orange
[3] => banana
)
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}
function test_print($item2, $key)
{
echo "$key. $item2
\n";}
echo "Before ...:\n";
array_walk($fruits, 'test_print');
array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";
array_walk($fruits, 'test_print');
php
$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');
function test_print($item, $key)
{
echo "$key holds $item\n";
}
array_walk_recursive($fruits, 'test_print');
?>
以上例程会输出:
a holds apple
b holds banana
sour holds lemon
注意上例中的键 'sweet' 并没有显示出来。任何其值为数组的键都不会被传递到回调函数中去。
Example #1 array_map() 例子
php
function cube($n)
{
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?>
这使得 $b 成为:
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
Example #2 array_map() - 使用更多的数组
php
function show_Spanish($n, $m)
{
return("The number $n is called $m in Spanish");
}
function map_Spanish($n, $m)
{
return(array($n => $m));
}
$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");
$c = array_map("show_Spanish", $a, $b);
print_r($c);
$d = array_map("map_Spanish", $a , $b);
print_r($d);
?>
以上例程会输出:
// printout of $c
Array
(
[0] => The number 1 is called uno in Spanish
[1] => The number 2 is called dos in Spanish
[2] => The number 3 is called tres in Spanish
[3] => The number 4 is called cuatro in Spanish
[4] => The number 5 is called cinco in Spanish
)
$name = 'caida';
$sax = 20;
$phone = '139...';
$arr = array('name', 'sax', 'phone');
$newarr = compact($arr);
print_r($newarr);
print_r(compact('name'));
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array);
echo $size; #medium
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
php
function odd($var)
{
return($var % 2 == 1);
}
function even($var)
{
return($var % 2 == 0);
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);
echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
?>
Odd :
Array
(
[a] => 1
[c] => 3
[e] => 5
)
Even:
Array
(
[0] => 6
[2] => 8
[4] => 10
[6] => 12
)
array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
$a= array(aa, bb, cc,);
foreach($a as $k=>$v){
$s[$v] = $a;
array_splice($s[$v], $k, 1);
}
print_r($s);
Array
(
[ aa ] => Array ([0] => bb
[1] => cc
)
[bb] => Array
([0] => aa
[1] => cc
)
[cc] => Array
([0] => aa
[1] => bb
)
)
只适用于索引数组。
1.索引下标会重建
$a= array(aa, bb, cc,);
array_splice($a,array_search('bb',$a),1);
print_r($a);
2.索引下标不会重建
$a = array ( aa , bb , cc ,);unset ( $a [ 1 ]);print_r ( $a );
适用于关联索引,删除关联数组用unset
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r($a);
unset($a[array_search("Cat",$a)]);//array_search("Cat",$a)按元素值返回键名。去除后保持索引
print_r($a);
$a = array(
array('60'=>61),
array('60'=>62),
array('61'=>63),
array('61'=>64),
array('62'=>65),
array('63'=>66),
);
方法1:
$arr = array();
foreach($a as $key=>$v){
foreach ($v as $k=>$vv) {
$arr[$k][]=$vv;
}
}
Array
(
[60] => Array
([0] => 61
[1] => 62
)
[61] => Array
([0] => 63
[1] => 64
)
[ 62 ] => Array ([0] => 65
)
[63] => Array
([0] => 66
)
)
方法2:
$result = array ();
foreach($a as $val) {
foreach($val as $k => $v) {
if( isset($result[$k])){
$result[$k] = $result[$k].','.$v;
}else{
$result [ $k ] = $v ;
}
}
}
结果:
Array
(
[ 60 ] = > 61 , 62
[ 61 ] = > 60 , 62
[ 62 ] = > 60 , 61
[ 63 ] = > 64
[ 64 ] = > 63
)
Array
(
[ 0 ] = > 60 , 61 , 62
[ 1 ] = > 61 , 60 , 62
[ 2 ] = > 62 , 60 , 61
[ 3 ] = > 63 , 64
[ 4 ] = > 64 , 63
)
处理函数:
//写的比较好function a_array_unique( $ array )
{$out = array ();
foreach( $ array as $key = > $value ) {
$arr = explode( ',' , $value );
sort( $arr );
$sovalue = join( ',' , $arr );
if ( ! in_array ( $sovalue , $out )) {
$out [ $key ] = $value ;
}
}
return$out ;
}
$ars = a_array_unique( $result );
结果:
Array
(
[ 0 ] = > 60 , 61 , 62
[ 3 ] = > 63 , 64
)
例如数组:Array ( [34] => 2 [7] => 100 ) PHP 如何获取值最大的键值 这个例子中应该返回7
$arr=array(34=>2,7=>100);//定义一个数组
$t=max($arr);//获取最大的值
$brr=array_flip($arr);//将数组键与值互换
echo $brr[$t];//输出结果,即为原数组最大值的键
$arr = array ( "a" , "b" , "c" , "d" );
function isHave ( $ var ){
if ( $ var != "b" ) return true ;
}
array_filter ( $arr , "isHave" );
function utf8_array_asort( & $ array ) {
if ( ! isset ( $ array ) || ! is_array( $ array )) {
return false ;
}
foreach ( $ array as $k = > $v ) {
$ array [ $k ] = iconv( 'UTF-8' , 'GBK//IGNORE' , $v );
}
asort( $ array );
foreach ( $ array as $k = > $v ) {
$ array [ $k ] = iconv( 'GBK' , 'UTF-8//IGNORE' , $v );
}
return true ; }