PHP implements array operations

小云云
Release: 2023-03-22 16:06:01
Original
2009 people have browsed it

This article mainly shares with you how to implement array operations in PHP, mainly in the form of text and code. I hope it can help you.

Statistical related

array_sum(array) Statistical array sum
array_product(array) Statistical array product

Example

// 已有字符串2,3,4,5,19,39
$str = '2,3,4,5,19,39';
$arr = explode(',',$str);
// 1. array_sum(数组)统计数组的和
echo array_sum($arr);
echo &#39;<br/>&#39;;
// 2.array_product(数组)统计数组的乘积 
echo array_product($arr);
echo &#39;<hr/>&#39;;
Copy after login
Copy after login

Array first elements&& last element

reset() first element
end() whether the last element

value is in the set

in_array()

Example

$ext = &#39;jpg&#39;;
$allowExts=[&#39;jpg&#39;,&#39;jpeg&#39;,&#39;gif&#39;,&#39;png&#39;];
var_dump(in_array($ext,$allowExts));
Copy after login

Pointer operation related

array_pop(数组)               删除数组最后一个
array_push(数组,添加元素)     数组末尾添加一个
array_shift(数组)             删除数组第一个
array_unshift(数组,添加元素)  数组开头添加一个
Copy after login

Array to string

implode((separator,)array)
join((separator,) )Array)

Example

// 把逗号与数组拼接成系新字符串$str1 = join(&#39;,&#39;,range(0,9));echo $str1;
拼接数组
array_merge(arr1,arr2,arr3...)
随机输出key
array_rand(数组(,长度))
交换key和value
array_flip()
Copy after login

Example

$arr = [&#39;a&#39;,&#39;b&#39;,&#39;c&#39;];
$newArr = array_flip($arr);
var_dump($newArr);
输出结果:
    array (size=3)
      666 => int 0
      &#39;txt&#39; => int 1
      &#39;jpg&#39; => int 2
    )
Copy after login
Copy after login

Statistics related

array_sum(array) The sum of statistical arrays
array_product(array) Statistical array The product

Example

// 已有字符串2,3,4,5,19,39
$str = &#39;2,3,4,5,19,39&#39;;
$arr = explode(&#39;,&#39;,$str);
// 1. array_sum(数组)统计数组的和
echo array_sum($arr);
echo &#39;<br/>&#39;;
// 2.array_product(数组)统计数组的乘积 
echo array_product($arr);
echo &#39;<hr/>&#39;;
Copy after login
Copy after login

The first element of the array && The last element

reset() The first element
end() The last element

Whether the value is in the collection

in_array()

Example

$ext = 'jpg';
$allowExts=['jpg','jpeg ','gif','png'];
var_dump(in_array($ext,$allowExts));

Pointer operation related

array_pop(array)                                                                                                                                                                                                                               ##array_push(array, add element) Add an
array_shift(array) at the end of the array Delete the first one in the array
array_unshift(array, add element) Add an

array to string

implode((separator,)array)

join((separator,)array)

Example

// 把逗号与数组拼接成系新字符串
$str1 = join(&#39;,&#39;,range(0,9));
echo $str1;
Copy after login

Splicing array

array_merge( arr1, arr2, arr3...)

Randomly output key

array_rand(array(,length))

Exchange key and value

array_flip( )

Example

$arr = [&#39;a&#39;,&#39;b&#39;,&#39;c&#39;];
$newArr = array_flip($arr);
var_dump($newArr);
输出结果:
    array (size=3)
      666 => int 0
      &#39;txt&#39; => int 1
      &#39;jpg&#39; => int 2
    )
Copy after login
Copy after login
Related recommendations:

js array operation example analysis

Points to note when operating arrays in JavaScript Basic

PHP array operation development examples and sharing of ideas

The above is the detailed content of PHP implements array operations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template