"PHP array columns()"의 두 판 사이의 차이

(새 문서: {{DISPLAYTITLE:PHP array_columns()}} ==개요== ;PHP array_columns() <source lang='php'> $records = [ ['id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe'], ['id' => 3245...)
 
3번째 줄: 3번째 줄:
;PHP array_columns()
;PHP array_columns()
<source lang='php'>
<source lang='php'>
function array_columns(array $rows, array $keys)
{
foreach($rows as $i => $row) {
$new_row = [];
foreach( $keys as $key ) $new_row[$key] = $row[$key];
$rows[$i] = $new_row;
}
return $rows;
}
$records = [
$records = [
  ['id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe'],
['id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe'],
  ['id' => 3245, 'first_name' => 'Sally', 'last_name' => 'Smith'],
['id' => 3245, 'first_name' => 'Sally', 'last_name' => 'Smith'],
  ['id' => 5342, 'first_name' => 'Jane', 'last_name' => 'Jones'],
['id' => 5342, 'first_name' => 'Jane', 'last_name' => 'Jones'],
];
];
 
$first_names = array_column($records, 'first_name');
print_r( array_columns($records,['first_name']) );
print_r($first_names);
# Array
# Array
# (
# (
#    [0] => John
#    [0] => Array
#    [1] => Sally
#        (
#    [2] => Jane
#            [first_name] => John
#        )
#    [1] => Array
#        (
#            [first_name] => Sally
#        )
#    [2] => Array
#        (
#            [first_name] => Jane
#        )
# )
print_r( array_columns($records,['first_name','last_name']) );
# Array
# (
#    [0] => Array
#        (
#            [first_name] => John
#            [last_name] => Doe
#        )
#
#    [1] => Array
#        (
#            [first_name] => Sally
#            [last_name] => Smith
#        )
#    [2] => Array
#        (
#            [first_name] => Jane
#            [last_name] => Jones
#        )
# )
# )
</source>
</source>

2018년 5월 6일 (일) 02:21 판

1 개요

PHP array_columns()
function array_columns(array $rows, array $keys)
{
	foreach($rows as $i => $row) {
		$new_row = [];
		foreach( $keys as $key ) $new_row[$key] = $row[$key];
		$rows[$i] = $new_row;
	}
	return $rows;
}

$records = [
	['id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe'],
	['id' => 3245, 'first_name' => 'Sally', 'last_name' => 'Smith'],
	['id' => 5342, 'first_name' => 'Jane', 'last_name' => 'Jones'],
];

print_r( array_columns($records,['first_name']) );
# Array
# (
#     [0] => Array
#         (
#             [first_name] => John
#         )
#     [1] => Array
#         (
#             [first_name] => Sally
#         )
#     [2] => Array
#         (
#             [first_name] => Jane
#         )
# )
print_r( array_columns($records,['first_name','last_name']) );
# Array
# (
#     [0] => Array
#         (
#             [first_name] => John
#             [last_name] => Doe
#         )
# 
#     [1] => Array
#         (
#             [first_name] => Sally
#             [last_name] => Smith
#         )
#     [2] => Array
#         (
#             [first_name] => Jane
#             [last_name] => Jones
#         )
# )

2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}