ฟังก์ชัน array_walk_recursice() ใช้ฟังก์ชันผู้ใช้แบบเรียกซ้ำกับสมาชิกทุกคนในอาร์เรย์
ไวยากรณ์
array_walk_recursive(arr, custom_func, parameter)
พารามิเตอร์
-
อาร์ − อาร์เรย์ที่ระบุ จำเป็น
-
custom_func − ฟังก์ชันที่ผู้ใช้กำหนด จำเป็น
-
พารามิเตอร์ − พารามิเตอร์ที่จะตั้งค่าสำหรับฟังก์ชันแบบกำหนดเอง ไม่บังคับ
คืนสินค้า
ฟังก์ชัน array_walk_recursive() จะคืนค่า TRUE เมื่อสำเร็จ หรือ FALSE เมื่อล้มเหลว
ตัวอย่าง
ต่อไปนี้เป็นตัวอย่าง −
<?php
function display($val,$key) {
echo "Key $key with the value $val<br>";
}
$arr1 = array("p"=>"accessories","q"=>"footwear");
$arr2 = array($arr1,"1"=>"electronics");
array_walk_recursive($arr2,"display");
?> ผลลัพธ์
Key p with the value accessories Key q with the value footwear Key 1 with the value electronics
ตัวอย่าง
ให้เราดูตัวอย่างอื่นที่ส่งผ่านพารามิเตอร์อื่น -
<?php
function display($val,$key, $extra) {
echo "Key $key $extra $val<br>";
}
$arr1 = array("p"=>"accessories","q"=>"footwear");
$arr2 = array($arr1,"5"=>"electronics");
array_walk_recursive($arr2,"display", "with value");
?> ผลลัพธ์
Key p with the value accessories Key q with the value footwear Key 5 with the value electronics