PHP array_column() 함수
예시
레코드세트에서 성의 열 가져오기:
<?php
// An array that represents a possible record set returned from a database
$a = array(
array(
'id' => 5698,
'first_name' => 'Peter',
'last_name' => 'Griffin',
),
array(
'id' => 4767,
'first_name' => 'Ben',
'last_name' => 'Smith',
),
array(
'id' => 3809,
'first_name' => 'Joe',
'last_name' => 'Doe',
)
);
$last_names = array_column($a, 'last_name');
print_r($last_names);
?>
산출:
Array
(
[0] => Griffin
[1] => Smith
[2] => Doe
)
정의 및 사용
array_column() 함수는 입력 배열의 단일 열에서 값을 반환합니다.
통사론
array_column(array, column_key, index_key)
매개변수 값
Parameter | Description |
---|---|
array | Required. Specifies the multi-dimensional array (record-set) to use. As of PHP 7.0, this can also be an array of objects. |
column_key | Required. An integer key or a string key name of the column of values to return. This parameter can also be NULL to return complete arrays (useful together with index_key to re-index the array) |
index_key | Optional. The column to use as the index/keys for the returned array |
기술적 세부 사항
반환 값: | 입력 배열에서 단일 열을 나타내는 값 배열을 반환합니다. |
---|---|
PHP 버전: | 5.5+ |
더 많은 예
예시
"id" 열로 인덱싱된 레코드 집합에서 성의 열을 가져옵니다.
<?php
// An array that represents a possible record set returned from a database
$a = array(
array(
'id' => 5698,
'first_name' => 'Peter',
'last_name' => 'Griffin',
),
array(
'id' => 4767,
'first_name' => 'Ben',
'last_name' => 'Smith',
),
array(
'id' => 3809,
'first_name' => 'Joe',
'last_name' => 'Doe',
)
);
$last_names = array_column($a, 'last_name', 'id');
print_r($last_names);
?>
산출:
Array
(
[5698] => Griffin
[4767] => Smith
[3809] => Doe
)
❮ PHP 배열 참조