1.反转函数的实现
array_reverse(array,preserve)
<?php
function array_rev($arr){
$n = count($arr);
$left = 0;
$right = $n-1;
while($left < $right){
$tmp = $arr[$left];
$arr[$left] = $arr[$right];
$left++;
$arr[$right] = $tmp;
$right--;
}
return $arr;
}
$a = array(1, 2, 3, 4);
$result = array_rev($a);
print_r($result);
Array ( [0] => 4 [1] => 3 [2] => 2 [3] => 1 )
?????(解决)所以数组都可以翻转,这里仅仅是从0开始的索引数组可以执行。
<?php
function array_rev($arr){
$kArr = array_keys($arr);
$vArr = array_values($arr);
$n = count($arr);
$left = 0;
$right = $n - 1;
while($left < $right){
$vtemp = $vArr[$left];
$ktemp = $kArr[$left];
$vArr[$left] = $vArr[$right];
$kArr[$left] = $kArr[$right];
$left++;
$vArr[$right] = $vtemp;
$kArr[$right] = $ktemp;
$right--;
}
return array_combine($kArr, $vArr);
}
$arr = array('a'=>'1', 3, 4, 'c'=>'22dsad');
print_r(array_rev($arr));
Array ( [c] => 22dsad [1] => 4 [0] => 3 [a] => 1 )
2.两个有序int数组是否有相同元素的最优算法
<?php
function array_common($arr1, $arr2){
$i = 0;
$j = 0;
$arr_common = [];
$n1 = count($arr1);
$n2 = count($arr2);
while($i < $n1 && $j < $n2){
if($arr1[$i] < $arr2[$j]){
$i++;
}elseif($arr1[$i] > $arr2[$j]){
$j++;
}else{
$arr_common[] = $arr1[$i];
$i++;
$j++;
}
}
return $arr_common;
}
$arr1 = array(1, 2, 3, 4);
$arr2 = array(3, 4, 5);
var_dump(array_common($arr1, $arr2));
array (size=2)
0 => int 3
1 => int 4
3.两个数组否有相同元素的最优算法
<?php
function array_common($arr1, $arr2){
$merge_array = array_merge($arr1, $arr2);
// var_dump($merge_array);
$str = implode('',$merge_array);
$new_arr = str_split($str);
// var_dump($new_arr);
$result = array_count_values($new_arr);
$arr_common = array();
foreach ($result as $k => $v) {
if($v >= 2){
$arr_common[] = $k;
}
}
return $arr_common;
}
$arr1 = array('a', 2, 3);
$arr2 = array(3, 4, 5, 'a');
var_dump(array_common($arr1, $arr2));
array (size=2)
0 => string 'a' (length=1)
1 => int 3
4.array_intersect(array1,array2,array3...);
比较两个数组的键值,并返回交集
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_intersect($a1,$a2);
print_r($result);
?>
Array ( [a] => red [b] => green [c] => blue )
5.PHP去除数组中重复数据的两个例子:一
<?php
$input = array("a" => "green","", "red","b" => "green", "","blue", "red","c" => "witer","hello","witer");
// $result = array_unique($input); //去除重复元素
$result = a_array_unique($input); //只留下单一元素
foreach($result as $a1)
{
echo $a1 . "<br />";
}
function a_array_unique($array)//写的比较好(写方法)
{
$out = array();
foreach($array as $k => $v){
if(!in_array($v,$out)){
$out[$k] = $v;
}
}
return $out;
}
green
red
blue
witer
hello
6.PHP去除数组中重复数据的两个例子:二
7.array_keys(array,value,strict)
返回包含数组中所有键名的一个新数组
<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a));
?>
Array ( [0] => Volvo [1] => BMW [2] => Toyota )
8.join(separator,array)
把数组元素组合为一个字符串
<?php
$arr = array('Hello','World!','I','love','Shanghai!');
echo join(" ",$arr);
?>
Hello World! I love Shanghai!
9.数组指针
<?php
$people = array("Bill", "Steve", "Mark", "David");
echo current($people) . "<br>"; // 当前元素是 Bill
echo next($people) . "<br>"; // Bill 的下一个元素是 Steve
echo current($people) . "<br>"; // 现在当前元素是 Steve
echo prev($people) . "<br>"; // Steve 的上一个元素是 Bill
echo end($people) . "<br>"; // 最后一个元素是 David
echo prev($people) . "<br>"; // David 之前的元素是 Mark
echo current($people) . "<br>"; // 目前的当前元素是 Mark
echo reset($people) . "<br>"; // 把内部指针移动到数组的首个元素,即 Bill
echo next($people) . "<br>"; // Bill 的下一个元素是 Steve
print_r (each($people)); // 返回当前元素的键名和键值(目前是 Steve),并向前移动内部指针
?>
Array ( [1] => Steve [value] => Steve [0] => 1 [key] => 1 )