PHP中的Array函數用于創建和操作數組。以下是一些常用的Array函數的用法:
示例:
$fruits = array('apple', 'banana', 'orange');
示例:
$count = count($fruits);
echo $count; // 輸出:3
示例:
array_push($fruits, 'grape', 'melon');
print_r($fruits); // 輸出:Array ( [0] => apple [1] => banana [2] => orange [3] => grape [4] => melon )
示例:
$lastFruit = array_pop($fruits);
echo $lastFruit; // 輸出:melon
示例:
$firstFruit = array_shift($fruits);
echo $firstFruit; // 輸出:apple
示例:
array_unshift($fruits, 'kiwi', 'mango');
print_r($fruits); // 輸出:Array ( [0] => kiwi [1] => mango [2] => banana [3] => orange )
示例:
$moreFruits = array('pineapple', 'watermelon');
$allFruits = array_merge($fruits, $moreFruits);
print_r($allFruits); // 輸出:Array ( [0] => kiwi [1] => mango [2] => banana [3] => orange [4] => pineapple [5] => watermelon )
示例:
$subset = array_slice($fruits, 2, 2);
print_r($subset); // 輸出:Array ( [0] => banana [1] => orange )
示例:
$key = array_search('banana', $fruits);
echo $key; // 輸出:1
這只是一些Array函數的用法示例,還有很多其他Array函數可以用來操作和處理數組數據。您可以查閱PHP官方文檔以獲取更多詳細信息和示例。