PHP中常用的数组函数非常多,以下是一些常用的数组函数及简单示例:
$fruits = array("apple", "banana", "cherry");
$fruits = array("apple", "banana", "cherry");
echo count($fruits); // 输出 3
$fruits = array("apple", "banana");
array_push($fruits, "cherry", "date");
print_r($fruits); // 输出Array ( [0] => apple [1] => banana [2] => cherry [3] => date )
$fruits = array("apple", "banana", "cherry");
$last_fruit = array_pop($fruits);
echo $last_fruit; // 输出 cherry
$fruits = array("apple", "banana", "cherry");
$first_fruit = array_shift($fruits);
echo $first_fruit; // 输出 apple
$fruits = array("apple", "banana");
array_unshift($fruits, "cherry", "date");
print_r($fruits); // 输出Array ( [0] => cherry [1] => date [2] => apple [3] => banana )
$fruits1 = array("apple", "banana");
$fruits2 = array("cherry", "date");
$all_fruits = array_merge($fruits1, $fruits2);
print_r($all_fruits); // 输出Array ( [0] => apple [1] => banana [2] => cherry [3] => date )
$fruits = array("apple", "banana", "cherry", "date", "elderberry");
$slice_fruits = array_slice($fruits, 1, 3);
print_r($slice_fruits); // 输出Array ( [0] => banana [1] => cherry [2] => date )
$fruits = array("apple", "banana", "cherry");
$key = array_search("banana", $fruits);
echo $key; // 输出 1
$fruits = array("apple", "banana", "cherry");
$result = in_array("banana", $fruits);
echo $result; // 输出 1