Hướng dẫn dùng array examples trong PHP

ParameterDescriptionarrayRequired. Specifies the array to work on.callbackRequired. Specifies the name of the user-defined callback function. The callback function typically takes on two parameters — the array value being the first, and the key/index second.userdataOptional. Specifies a parameter to the user-defined callback function. It will be passed as the third parameter to the callback function.

Hướng dẫn cách sử dụng hàm array_values[] về mảng trong lập trình PHP

Tác dụng của hàm array_values[]

The array_values[] function returns an array containing all the values of an array.

The returned array will be indexed numerically, starting with 0 and increase each time by 1.

The following table summarizes the technical details of this function.

Return Value:Returns an indexed array of all the values of an array.Version:PHP 4+

Syntax

The basic syntax of the array_values[] function is given with:

The following example shows the array_values[] function in action.

"apple", "b"=>"ball", "c"=>"cat", "d"=>"dog"];

// Getting all the values from alphabets array
$result = array_values[$alphabets];
print_r[$result];
?>

Parameters

The array_values[] function accepts the following parameters.

ParameterDescriptionarrayRequired. Specifies the array to work on.

More Examples

Here're some more examples showing how array_values[] function actually works:

This function may ignore the numeric indexes, let's try out an example and see how it works:

10, 3=>25, 1=>50, 2=>75];

// Adding a value to the numbers array at index 0
$numbers[0] = 100;

// Getting all the values from numbers array
$result = array_values[$numbers];
print_r[$result];
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

như ví dụ ở trên ta dựa vào hàm strlen để nhóm các phần tử có cùng số kí tự vào một nhóm với key là số kí tự.

  • hasDuplicates kiểm tra danh sách các phần tử trong mảng nếu có ít nhất hai giá trị phần tử trùng lặp thì trả về true còn các giá trị của mảng là duy nhất thì trả về false
all[[2, 3, 4, 5], function [$item] {
    return $item > 1;
}]; // true
4

ví dụ

all[[2, 3, 4, 5], function [$item] {
    return $item > 1;
}]; // true
5
  • head trả về giá trị phần tử đầu tiên của mảng
all[[2, 3, 4, 5], function [$item] {
    return $item > 1;
}]; // true
6

ví dụ

all[[2, 3, 4, 5], function [$item] {
    return $item > 1;
}]; // true
7
  • last trả về giá trị phần tử cuối cùng của mảng
all[[2, 3, 4, 5], function [$item] {
    return $item > 1;
}]; // true
8

ví dụ

all[[2, 3, 4, 5], function [$item] {
    return $item > 1;
}]; // true
9
  • pluck lấy tất cả giá trị có chung key đã cho sẵn
function any[$items, $func]
{
    return count[array_filter[$items, $func]] > 0;
}
0

ví dụ

function any[$items, $func]
{
    return count[array_filter[$items, $func]] > 0;
}
1

ở ví dụ trên hàm pluck trả về một mảng có chung key là 'name' mà ta truyền vào.

  • pull để lọc ra các giá trị được truyền vào.
function any[$items, $func]
{
    return count[array_filter[$items, $func]] > 0;
}
2

ví dụ

function any[$items, $func]
{
    return count[array_filter[$items, $func]] > 0;
}
3

ở ví dụ trên khi truyền vào hàm pull 2 giá trị 'a', 'c' thì hai giá trị này đã bị loại bỏ ra khỏi mảng.

Chủ Đề