<?php
function format(&$item) {
$item = strtoupper($item) . '.';
return $item; //hàm này trả về item được in hoa
}
$shopping = array("fish", "bread", "eggs", "jelly", "apples");
array_walk($shopping, "format"); //hàm array_walk nhận 2 tham số, hai tham số này ngược với array_map.
$shopping = sort($shopping); //tricky ở chỗ này, vì hàm sort() trả về boolean nên sẽ là 1 nếu print hay echo.
echo $shopping[1]; //shopping không còn là array nữa nên đâu có trả về cái giề, vãi thật.
/*--------------------------------------*/
echo "------------code sửa một tí, ra kết quả khác--------";
//code dưới copy câu trên chỉ sửa một tí
$shopping = array("fish", "bread", "eggs", "jelly", "apples");
array_walk($shopping, "format"); //hàm array_walk nhận 2 tham số, hai tham số này ngược với array_map.
sort($shopping); // mình không đặt biến để hứng kết quả từ hàm sort, vì nếu không nó sẽ trả về true/1
echo $shopping[1]; //giờ thì ra bread viết hoa cho mà xem, vì hàm sort sắp xếp theo abcd/
//by @vuong
Warning: Trying to access array offset on true in /in/rt1LS on line 13
------------code sửa một tí, ra kết quả khác--------BREAD.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Warning: Trying to access array offset on true in /in/rt1LS on line 13
------------code sửa một tí, ra kết quả khác--------BREAD.