20170926

1.反转函数的实现

array_reverse(array,preserve)

Array ( [0] => 4 [1] => 3 [2] => 2 [3] => 1 )

?????(解决)所以数组都可以翻转,这里仅仅是从0开始的索引数组可以执行。

'1', 3, 4, 'c'=>'22dsad');
print_r(array_rev($arr));

Array ( [c] => 22dsad [1] => 4 [0] => 3 [a] => 1 )

2.两个有序int数组是否有相同元素的最优算法

 $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.两个数组否有相同元素的最优算法

 $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...);

比较两个数组的键值,并返回交集

"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去除数组中重复数据的两个例子:一

 "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 . "
"; } 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)

返回包含数组中所有键名的一个新数组

"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a));
?>

Array ( [0] => Volvo [1] => BMW [2] => Toyota )

8.join(separator,array)

把数组元素组合为一个字符串


Hello World! I love Shanghai!

9.数组指针

"; // 当前元素是 Bill
echo next($people) . "
"; // Bill 的下一个元素是 Steve echo current($people) . "
"; // 现在当前元素是 Steve echo prev($people) . "
"; // Steve 的上一个元素是 Bill echo end($people) . "
"; // 最后一个元素是 David echo prev($people) . "
"; // David 之前的元素是 Mark echo current($people) . "
"; // 目前的当前元素是 Mark echo reset($people) . "
"; // 把内部指针移动到数组的首个元素,即 Bill echo next($people) . "
"; // Bill 的下一个元素是 Steve print_r (each($people)); // 返回当前元素的键名和键值(目前是 Steve),并向前移动内部指针 ?>

Array ( [1] => Steve [value] => Steve [0] => 1 [key] => 1 )

你可能感兴趣的:(20170926)