"PHP transpose()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(다른 사용자 한 명의 중간 판 하나는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;PHP transpose()
;PHP transpose()
<source lang='php'>
<syntaxhighlight lang='php'>
function transpose($rows) {
function transpose($rows) {
$result = [];
$result = [];
$keys = array_keys($rows[0]);
$keys = array_keys($rows[0]);
foreach( $keys as $key ) {
foreach( $keys as $key ) {
        foreach( $rows as $row ) $result[$key][] = $row[$key];
foreach( $rows as $row ) $result[$key][] = $row[$key];
}
}
return $result;
return $result;
38번째 줄: 38번째 줄:
#        )
#        )
# )
# )
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:35 기준 최신판

1 개요[ | ]

PHP transpose()
function transpose($rows) {
	$result = [];
	$keys = array_keys($rows[0]);
	foreach( $keys as $key ) {
		foreach( $rows as $row ) $result[$key][] = $row[$key];
	}
	return $result;
}

$members = [
	['id'=>102, 'name'=>'Ashley Allen', 'address'=>'Seoul'],
	['id'=>202, 'name'=>'Peter Parker', 'address'=>'New York'],
	['id'=>104, 'name'=>'John Smith', 'address'=>'Tokyo'],
];
print_r( transpose($members) );
# Array
# (
#     [id] => Array
#         (
#             [0] => 102
#             [1] => 202
#             [2] => 104
#         )
#     [name] => Array
#         (
#             [0] => Ashley Allen
#             [1] => Peter Parker
#             [2] => John Smith
#         )
#     [address] => Array
#         (
#             [0] => Seoul
#             [1] => New York
#             [2] => Tokyo
#         )
# )

2 같이 보기[ | ]

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