编程语言
首页 > 编程语言> > PHP笔记(三):数组相关操作方法

PHP笔记(三):数组相关操作方法

作者:互联网

本文记录一些PHP数组处理的方法。

目录

1. 检查数组中是否存在某个值

in_array(mixed $needle, array $haystack, bool $strict = false): bool

举例:

<?php
    
/* in_array */
$array1 = ['one', 'two', 'three', 'four', 'five'];
if (in_array("one", $array1)) {
    echo "Got one";
}
?>

结果:

Got one

2. 检查数组或者变量方法

变量检查:

数组检查:

3. 过滤数组元素

array_filter(array $array, ?callable $callback = null, int $mode = 0): array

举例:

<?php

/* array_filter */
$array1 = [0 => '', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five'];
$array1 = array_filter($array1);
echo "<pre>";
print_r($array1);
echo "</pre>";

/* array_values */
$array1 = array_values($array1);  // 重新索引
echo "<pre>";
print_r($array1);
echo "</pre>";


function odd($var)
{
    return $var & 1;
}
function even($var)
{
    return !($var & 1);
}

$array1 = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5];
$array_even = array_filter($array1, "even");
$array_odd = array_filter($array1, "odd");
echo "<pre>";
print_r($array_even);
echo "</pre>";
echo "<pre>";
print_r($array_odd);
echo "</pre>";


$array2 = [1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five'];
$array_even = array_filter($array2,function($var){ return(!($var & 1));},ARRAY_FILTER_USE_KEY);
$array_odd = array_filter($array2,function($var){ return($var & 1);},ARRAY_FILTER_USE_KEY);
echo "<pre>";
print_r($array_even);
echo "</pre>";
echo "<pre>";
print_r($array_odd);
echo "</pre>";
?>

结果:

Array
(
    [1] => one
    [2] => two
    [3] => three
    [4] => four
    [5] => five
)
Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
)
Array
(
    [two] => 2
    [four] => 4
)
Array
(
    [one] => 1
    [three] => 3
    [five] => 5
)
Array
(
    [2] => two
    [4] => four
)
Array
(
    [1] => one
    [3] => three
    [5] => five
)

4. 删除数组元素

array_pop(array &$array) - 删除数组最后一个元素

举例:

<?php
    
/* array_pop */
$array1 = ['one', 'two', 'three', 'four', 'five'];
$last = array_pop($array1);
echo "<pre>";
print_r($array1);
echo "</pre>";

?>

结果:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)

array_shift(array &$array) - 将数组开头的单元移出数组并作为结果返回

举例:

<?php
    
/* array_shift */
$array1 = ['one', 'two', 'three', 'four', 'five'];
$first = array_shift($array1);
echo "<pre>";
print_r($array1);
echo "</pre>";

?>

结果:

Array
(
    [0] => two
    [1] => three
    [2] => four
    [3] => five
)

5. 插入数组元素

array_unshift(&$array, ...$values) - 在数组开头插入一个或多个元素

举例:

<?php    
/* array_unshift */
$array1 = ['one', 'two', 'three', 'four', 'five'];
array_unshift($array1, '0', 'zero');
echo "<pre>";
print_r($array1);
echo "</pre>";

?>

结果:

Array
(
    [0] => 0
    [1] => zero
    [2] => one
    [3] => two
    [4] => three
    [5] => four
    [6] => five
)

array_push(&$array, ...$values) - 将一个或多个单元压入数组的末尾(入栈)

建议直接使用 $array[] = 追加元素。

举例:

<?php    
/* array_push */
$array1 = ['one', 'two', 'three', 'four', 'five'];
array_push($array1, 'six', 'seven');
$array1[] = 'eight';
echo "<pre>";
print_r($array1);
echo "</pre>";

?>

结果:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
    [5] => six
    [6] => seven
    [7] => eight
)

array_merge(...$arrays) — 合并一个或多个数组

举例:

<?php    
/* array_merge */
$array1 = ['one', 'two', 'three'];
$array2 = ['four', 'five'];
$array3 = array_merge($array1, $array2);
$array1[] = 'eight';
echo "<pre>";
print_r($array3);
echo "</pre>";

?>

结果:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
)

6. 返回数组最后一个元素

end(&$array) - 将 array 的内部指针移动到最后一个单元并返回其值。

array_key_last($array) - 获取一个数组的最后一个键值

举例:

<?php
/* end */
$array1 = ['one', 'two', 'three'];
$last = end($array1);
echo "<pre>";
print_r($last);
echo "</pre>";

echo current($array1) . "<br />\n"; // 返回数组中的当前值
reset($array1); // 将数组的内部指针指向第一个单元
echo current($array1) . "<br />\n";
next($array1); // 将数组的内部指针向后移动一位
echo current($array1) . "<br />\n";
prev($array1); // 将数组的内部指针向前移动一位
echo current($array1) . "<br />\n";

/* array_key_last */
$array1 = ['one', 'two', 'three'];
$last = array_key_last($array1);
echo "<pre>";
print_r($last);
echo "</pre>";
    
?>

结果:

three
three
one
two
one
2

7. 数组切片

array_slice( $array, $offset, $length = null, $preserve_keys = false
) - 返回根据 offsetlength 参数所指定的 array 数组中的一段序列。

举例:

<?php
/* array_slice */
$array1 = ['one', 'two', 'three', 'four', 'five'];
$output = array_slice($array1, 2);  
print_r($output) ;
echo "<br />\n";

$output = array_slice($array1, -3, 2);  // 返回 "d"
print_r($output) ;
echo "<br />\n";

$output = array_slice($array1, 0, 3); 
print_r($output) ;
echo "<br />\n";

$output = array_slice($array1, 1, -3); 
print_r($output) ;
echo "<br />\n";

$output = array_slice($array1, 1, 3); 
print_r($output) ;
echo "<br />\n";

$output = array_slice($array1, 1, 3, true); 
print_r($output) ;
echo "<br />\n";
?>

结果:

Array ( [0] => three [1] => four [2] => five )
Array ( [0] => three [1] => four )
Array ( [0] => one [1] => two [2] => three )
Array ( [0] => two )
Array ( [0] => two [1] => three [2] => four )
Array ( [1] => two [2] => three [3] => four )

8. 判断两个数组是否相等

array_diff( $array, ...$arrays) - 返回在 array 中但是不在其他 array 里的值。可用于判断两个数组是否相等。

array_diff_key( $array, ...$arrays) - 返回在 array 中但是未出现在任何其它数组中的键名的值。

举例:

<?php
/* array_diff */
$array1 = ['one', 'two', 'three'];
$array2 = ['one', 'two', 'three', 'four', 'five'];
$output1 = array_diff($array1, $array2);
$output2 = array_diff($array2, $array1);
if (empty($output1) and empty($output2)) {
    echo "The arrays are equal";
} else {
    echo "The arrays are not equal";
}

?>

结果:

The arrays are not equal

9. 替换数组元素

array_replace( $array, ...$replacements) - 使用传递的数组替换第一个数组的元素

举例:

<?php
/* array_replace */
$array1 = ['one', 'two', 'three'];
$array2 = ['four', 'five', 'six'];
$output = array_replace($array1, $array2);
echo "<pre>";
print_r($output);
echo "</pre>";

$array1 = ['one', 'two', 'three', '', '', ''];
$array2 = ['', '', '', 'four', 'five', 'six'];
$output = array_replace($array1,array_slice($array2,3,null,$preserve_keys = true));
echo "<pre>";
print_r($output);
echo "</pre>";

?>

结果:

Array
(
    [0] => four
    [1] => five
    [2] => six
)
Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
    [5] => six
)

10. 移除数组重复元素

array_unique - 移除数组中重复的值

举例:

<?php
/* array_unique */
$array1 = ['one', 'two', 'three', 'four', 'five', 'five'];
$output = array_unique($array1);
echo "<pre>";
print_r($output);
echo "</pre>";

?>

结果:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
)

11. 数组排序

常用排序函数如下表:

函数名称 排序依据 数组索引键保持 排序的顺序
array_multisort() string 键保持不变,int 键重新索引 第一个数组或者由选项指定
asort() 升序
arsort() 降序
krsort() 降序
ksort() 升序
natcasesort() 自然排序,大小写不敏感
natsort() 自然排序
rsort() 降序
shuffle() 随机
sort() 升序
uasort() 由用户定义
uksort() 由用户定义
usort() 由用户定义

举例:

<?php
/* asort */
$array1 = ['two' => 2, 'one' => 1, 'four' => 4, 'three' => 3, 'five' => 5];
asort($array1);
echo "<pre>";
print_r($array1);
echo "</pre>";

/* ksort */
$array1 = [1 => 'one', 3 => 'three', 2 => 'two', 5 => 'five', 4 => 'four'];
ksort($array1);
echo "<pre>";
print_r($array1);
echo "</pre>";

?>

结果:

Array
(
    [one] => 1
    [two] => 2
    [three] => 3
    [four] => 4
    [five] => 5
)
Array
(
    [1] => one
    [2] => two
    [3] => three
    [4] => four
    [5] => five
)

参考资料:

  1. 数组排序:https://www.php.net/manual/zh/array.sorting.php
  2. https://www.php.net/manual/zh/book.array.php
--THE END--

凡事都应当尽可能地简单,而不是较为简单。 ——爱因斯坦

标签:操作方法,array1,three,笔记,echo,four,数组,array,PHP
来源: https://www.cnblogs.com/hiyong/p/16182821.html