Câu hỏi phỏng vấn PHP
Câu hỏi

What exactly is the the difference between array_map , array_walk and array_filter ?

Câu trả lời

Here is an explanation of the differences between array_map(), array_walk(), and array_filter() in PHP:

array_map()

  • Applies a callback function to each element of an array and returns a new array with the transformed elements
  • The callback function is applied to each element in turn and the return value from the callback is placed at the corresponding index in the new array
  • The original array is not modified
  • Example:
php Copy
$numbers = [1, 2, 3, 4, 5];
$squared = array_map(function($x) {
    return $x * $x;
}, $numbers);
// $squared is now [1, 4, 9, 16, 25]

array_walk()

  • Applies a callback function to each element of an array
  • The callback function is applied to each element in turn, but the return value from the callback is ignored
  • The original array is modified if the callback function modifies the elements
  • Example:
php Copy
$fruits = ['apple', 'banana', 'orange'];
array_walk($fru...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

Why do we use extract() ?

middle

What is the difference between PDO's query() vs execute() ?

senior

What's better at freeing memory with PHP: unset() or $var = null ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào