PHP 从对应的计数数组创建多维数组
PHP Create Multidimensional Array from Corresponding Counts Array
将 PHP (7.1) 与以下数据和嵌套循环结合使用,我试图让每个主机与 COUNTS 数组中的相应数字相匹配。
HOSTS:
Array (
0 => 'example/search?results1'
1 => 'thisone/search?results2'
2 => 'thesetoo/search?results3'
)
COUNTS:
Array (
0 => '3'
1 => '5'
2 => '7'
)
foreach ( $counts as $count ) {
foreach ( $hosts as $host ) {
$t = $count;
for ($n=0; $n<$t; $n++) {
$results[] = ++$host;
}
continue 2;
}
}
echo 'THESE ARE ALL THE RESULTS:',PHP_EOL,PHP_EOL,var_dump($results);
我正在寻找的结果:
多维数组
Array (
0 => Array (
0 => 'example/search?results1'
1 => 'example/search?results1'
2 => 'example/search?results1'
)
1 => Array (
0 => 'thisone/search?results2'
1 => 'thisone/search?results2'
2 => 'thisone/search?results2'
3 => 'thisone/search?results2'
4 => 'thisone/search?results2'
)
2 => Array (
0 => 'thesetoo/search?results3'
1 => 'thesetoo/search?results3'
2 => 'thesetoo/search?results3'
3 => 'thesetoo/search?results3'
4 => 'thesetoo/search?results3'
5 => 'thesetoo/search?results3'
6 => 'thesetoo/search?results3'
)
)
注意每个 HOSTS 的结果数对应于 COUNTS 数组。
在上面的嵌套 for 循环中,我要么只为所有计数获取一个主机,要么为一维数组中的所有主机获取每个计数。我需要的是一个多维数组,但嵌套的 for 循环逻辑让我望而却步。我试过继续和打破循环,但没有运气。如果循环获得另一个计数,则它会跳过主机。如果它获得另一台主机,则它会跳过计数。
主机或计数数组都没有模式。这些将始终相互对应,但它们将是随机的 strings/numbers。感谢您的帮助。
如果 $hosts
和 $counts
的计数等于:
$result = [];
foreach ($hosts as $i => $host) {
$result[] = array_fill(0, $counts[$i], $host);
}
这个问题是 array_map()
and array_fill()
的完美用法示例。
$hosts = array(
0 => 'example/search?results1',
1 => 'thisone/search?results2',
2 => 'thesetoo/search?results3',
);
$counts = array(
0 => '3',
1 => '5',
2 => '7',
);
$result = array_map(
function($host, $count) {
return array_fill(0, $count, $host);
},
$hosts,
$counts
);
试试这个:
$hosts = array (
0 => 'example/search?results1',
1 => 'thisone/search?results2',
2 => 'thesetoo/search?results3'
);
$counts = array (
0 => '3',
1 => '5',
2 => '7'
);
$results =array();
foreach ( $counts as $count ) {
$key_of_count = array_search( $count, $counts );
for ($i=0; $i < (int)$count; $i++) {
$results[$key_of_count][] = $hosts[$key_of_count];
}
}
echo "<pre>"; print_r($results); echo "</pre>";
如果您正在寻找只使用循环而不使用任何花哨的数组函数的方法,那么这可能就是您要寻找的答案:
$result = [];
foreach($counts as $k=>$count){
$result[$k]='';
for($i=0; $i < $count; $i++){
$result[$k][] = $hosts[$k];
}
}
将 PHP (7.1) 与以下数据和嵌套循环结合使用,我试图让每个主机与 COUNTS 数组中的相应数字相匹配。
HOSTS:
Array (
0 => 'example/search?results1'
1 => 'thisone/search?results2'
2 => 'thesetoo/search?results3'
)
COUNTS:
Array (
0 => '3'
1 => '5'
2 => '7'
)
foreach ( $counts as $count ) {
foreach ( $hosts as $host ) {
$t = $count;
for ($n=0; $n<$t; $n++) {
$results[] = ++$host;
}
continue 2;
}
}
echo 'THESE ARE ALL THE RESULTS:',PHP_EOL,PHP_EOL,var_dump($results);
我正在寻找的结果: 多维数组
Array (
0 => Array (
0 => 'example/search?results1'
1 => 'example/search?results1'
2 => 'example/search?results1'
)
1 => Array (
0 => 'thisone/search?results2'
1 => 'thisone/search?results2'
2 => 'thisone/search?results2'
3 => 'thisone/search?results2'
4 => 'thisone/search?results2'
)
2 => Array (
0 => 'thesetoo/search?results3'
1 => 'thesetoo/search?results3'
2 => 'thesetoo/search?results3'
3 => 'thesetoo/search?results3'
4 => 'thesetoo/search?results3'
5 => 'thesetoo/search?results3'
6 => 'thesetoo/search?results3'
)
)
注意每个 HOSTS 的结果数对应于 COUNTS 数组。
在上面的嵌套 for 循环中,我要么只为所有计数获取一个主机,要么为一维数组中的所有主机获取每个计数。我需要的是一个多维数组,但嵌套的 for 循环逻辑让我望而却步。我试过继续和打破循环,但没有运气。如果循环获得另一个计数,则它会跳过主机。如果它获得另一台主机,则它会跳过计数。
主机或计数数组都没有模式。这些将始终相互对应,但它们将是随机的 strings/numbers。感谢您的帮助。
如果 $hosts
和 $counts
的计数等于:
$result = [];
foreach ($hosts as $i => $host) {
$result[] = array_fill(0, $counts[$i], $host);
}
这个问题是 array_map()
and array_fill()
的完美用法示例。
$hosts = array(
0 => 'example/search?results1',
1 => 'thisone/search?results2',
2 => 'thesetoo/search?results3',
);
$counts = array(
0 => '3',
1 => '5',
2 => '7',
);
$result = array_map(
function($host, $count) {
return array_fill(0, $count, $host);
},
$hosts,
$counts
);
试试这个:
$hosts = array (
0 => 'example/search?results1',
1 => 'thisone/search?results2',
2 => 'thesetoo/search?results3'
);
$counts = array (
0 => '3',
1 => '5',
2 => '7'
);
$results =array();
foreach ( $counts as $count ) {
$key_of_count = array_search( $count, $counts );
for ($i=0; $i < (int)$count; $i++) {
$results[$key_of_count][] = $hosts[$key_of_count];
}
}
echo "<pre>"; print_r($results); echo "</pre>";
如果您正在寻找只使用循环而不使用任何花哨的数组函数的方法,那么这可能就是您要寻找的答案:
$result = [];
foreach($counts as $k=>$count){
$result[$k]='';
for($i=0; $i < $count; $i++){
$result[$k][] = $hosts[$k];
}
}