Last active
February 2, 2017 01:16
-
-
Save tadasy/85ca0aa82c7214adbebe6eda52e57e17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 複数の配列から一つずつ要素を取得して、指定した個数分要素を取得したら終了 | |
*/ | |
$ary = [ | |
'a' => ['a1', 'a2', 'a3', 'a4'], | |
'b' => ['b1', 'b2', 'b3'], | |
'c' => ['c1', 'c2'], | |
'd' => ['d1', 'd2', 'd3', 'd4'], | |
]; | |
$keys = array_keys($ary); | |
$cnt = 0; | |
$max = 11; // 11個の要素を取得する | |
$out = []; | |
while (true) { | |
if ($cnt === $max) break; | |
$key = current($keys); | |
if (!isset($out[$key])) $out[$key] = []; | |
$elm = array_shift($ary[$key]); | |
if ($elm === null) { | |
// 配列にもう要素がない | |
if (next($keys)) { | |
continue; | |
} else { | |
reset($keys); | |
continue; | |
} | |
} else { | |
$out[$key][] = $elm; | |
$cnt++; | |
if (next($keys)) { | |
continue; | |
} else { | |
reset($keys); | |
continue; | |
} | |
} | |
} | |
var_dump($out); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment