Home >Backend Development >PHP Tutorial >PHP implementation skills for adding, deleting, calculating, reversing, sorting, searching and other operations on arrays

PHP implementation skills for adding, deleting, calculating, reversing, sorting, searching and other operations on arrays

墨辰丷
墨辰丷Original
2018-05-29 10:56:151106browse

This article mainly introduces PHP array operations, combined with examples to analyze PHP's implementation skills for adding, deleting, calculating, reversing, sorting, searching and other operations on arrays. Friends in need can refer to it

This article analyzes PHP array operations with examples. Share it with everyone for your reference, the details are as follows:

PHP’s array is a very important part. The operation examples are as follows:

<?php
function br() {
  echo &#39;<br />===============================================<br />&#39;;
}
$arr1 = array();
$arr1[] = &#39;x&#39;;
$arr1[] = &#39;a&#39;;
$arr1[] = &#39;e&#39;;
$arr1[] = &#39;c&#39;;
$arr1[] = &#39;h&#39;;
// 添加数组
array_push($arr1, 3, 23, 55);
// 数组长度
echo &#39;the size of array is :&#39;. count($arr1).&#39;<br />&#39;;
// 反转
var_dump(array_reverse($arr1));
// 排序 - 直接作用于数组
sort($arr1);
var_dump($arr1);
// 排序 - 按字符串排序
sort($arr1, SORT_STRING);
var_dump($arr1);
// 范围
$arr2 = range(&#39;a&#39;,&#39;h&#39;);
// 连接
$arrTemp1 = implode(&#39;-&#39;, $arr2);
echo $arrTemp1;
echo &#39;<br />&#39;;
// 切割
echo &#39;[&#39;.implode(&#39;][&#39;,array_reverse( explode(&#39;-&#39;, $arrTemp1) )).&#39;]&#39;;
// 数组合并,会重排索引
$arr3 = array_merge($arr1, $arr2);
var_dump($arr3);
// 删除数组元素
array_shift($arr3);
array_pop($arr3);
unset($arr3[4]);
array_splice($arr3, 6, 2);
var_dump($arr3);
// 抽取数组,原数组不变
$arr4 = array_slice($arr3, 2,3);
var_dump($arr4);
// 关联数组
$fruits = array(&#39;red&#39;=>&#39;apple&#39;, &#39;yellow&#39;=>&#39;banana&#39;, &#39;green&#39;=>&#39;lime&#39;);
// 数组键
$colors = array_keys($fruits);
// 数组值
$fla = array_values($fruits);
var_dump($colors);
var_dump($fla);
// 查找
echo in_array(&#39;green&#39;, $colors);
echo &#39;<br />&#39;;
echo in_array(&#39;black&#39;, $colors)? &#39;in&#39;:&#39;not in&#39;;
echo &#39;<br />&#39;;
echo array_key_exists(&#39;yellow&#39;, $fruits);
echo &#39;<br />&#39;;
// 按键排序
ksort($fruits);
var_dump($fruits);
// 按值排序
asort($fruits);
var_dump($fruits);
// 循环
foreach ($fruits as $key => $value) {
  echo $key. &#39; => &#39;.$value.&#39;<br />&#39;;
}
echo &#39;<br />&#39;;
$f = $fruits;
while($elem = each($f)) {
  echo $elem[&#39;key&#39;]. &#39; -- &#39;. $elem[&#39;value&#39;].&#39;<br />&#39;;
}
echo &#39;<br />&#39;;
$arr5 = array(2, 8, 100, 33, -18);
// 查找最大最小值
echo max($arr5);
echo &#39;<br />&#39;;
echo min($arr5);
echo &#39;<br />&#39;;
echo array_sum( $arr5 );
echo &#39;<br />&#39;;
function double($x) {
  echo ($x * 2).&#39; &#39;;
}
// 数组元素应用函数
array_walk($arr5, &#39;double&#39;);
function check($x) {
  return $x > 20;
}
// 筛选
var_dump(array_filter($arr5, &#39;check&#39;));
$arr6 = range(1,10);
echo &#39;random number: &#39;.array_rand($arr6);
//统计
//count(); sizeof(); array_count_values();
$arr7 = array(4,5,1,2,3,1,2,1);
$ac = array_count_values($arr7);
// 统计每个value出现的次数
var_dump($ac);
$arr8 = array(&#39;key1&#39;=>&#39;v1&#39;, &#39;key2&#39;=>&#39;v2&#39;, &#39;key3&#39;=>&#39;v3&#39;);
extract($arr8);
echo "$key1 $key2 $key3";
//填补
$input = array(12,10,9);
var_dump(array_pad($input, 5, 0));
var_dump(array_pad($input, -5, 0));
?>

The above is the entire content of this article, I hope it will be helpful to everyone's learning.


Related recommendations:

PHP implementation of using mysqli to operate MySQLdatabase

PHP uses mysqli to operate MySQLDatabaseMethod

##Use non-buffering mode in PHP to implement queryDatabaseMethods

The above is the detailed content of PHP implementation skills for adding, deleting, calculating, reversing, sorting, searching and other operations on arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn