"PHP 배열 array column()"의 두 판 사이의 차이

2번째 줄: 2번째 줄:
==개요==
==개요==
;PHP array_column()
;PHP array_column()
<source lang='php' run>
$orders = [
['id'=>1, 'name'=>'Ashley', 'date'=>'2020-05-20'],
['id'=>2, 'name'=>'Peter' , 'date'=>'2020-05-20'],
['id'=>3, 'name'=>'Ashley', 'date'=>'2020-05-05'],
['id'=>4, 'name'=>'John'  , 'date'=>'2020-05-05'],
['id'=>5, 'name'=>'Peter' , 'date'=>'2020-05-05'],
];
$names = array_column($orders, 'name');
print_r( $names );
</source>


<source lang='php' run>
<source lang='php' run>

2020년 5월 3일 (일) 01:20 판

1 개요

PHP array_column()
$orders = [
	['id'=>1, 'name'=>'Ashley', 'date'=>'2020-05-20'],
	['id'=>2, 'name'=>'Peter' , 'date'=>'2020-05-20'],
	['id'=>3, 'name'=>'Ashley', 'date'=>'2020-05-05'],
	['id'=>4, 'name'=>'John'  , 'date'=>'2020-05-05'],
	['id'=>5, 'name'=>'Peter' , 'date'=>'2020-05-05'],
];
$names = array_column($orders, 'name');
print_r( $names );
$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'],
];
 
$first_names = array_column($records, 'first_name');
print_r($first_names);
# Array
# (
#     [0] => John
#     [1] => Sally
#     [2] => Jane
# )
$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'],
];
 
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
# Array
# (
#     [2135] => Doe
#     [3245] => Smith
#     [5342] => Jones
#     [5623] => Doe
# )

2 같이 보기

3 참고

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