PHP array_columns()

1 개요[ | ]

PHP array_columns()
PHP array_select()
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 }}