我正在尝试根据某些变量创建一个重复值的数组。在这种情况下:$slipszero = "2"
& $slipsone = "1"
,这些值可以改变
这是我现在的代码:
$problist = array();
foreach ($rows as $value) {
if ($value['f_count'] == 0) {
$placeholder = rtrim(str_repeat($value["uid"] . ',', $slipszero), ', ') ;
array_push($problist, $placeholder);
// The above array_push should act just like:
array_push($problist, $value['uid'], $value['uid']);
} elseif ($value['f_count'] == 1) {
$placeholder = rtrim(str_repeat($value["uid"] . ',', $slipsone), ', ') ;
array_push($problist, $placeholder);
} elseif ($value['f_count'] >= 2) {
$problist[] = $value['uid'];
}
}
所以,从这个数组开始:
Array (
[0] => Array ( [uid] => 105 [f_count] => 0 )
[1] => Array ( [uid] => 106 [f_count] => 1 )
[2] => Array ( [uid] => 107 [f_count] => 0 )
[3] => Array ( [uid] => 108 [f_count] => 1 )
[4] => Array ( [uid] => 109 [f_count] => 2 )
)
我想最终得到这个数组:
array(15) {
[0] => string(3) "105"
[1] => string(3) "105"
[2] => string(3) "106"
[3] => string(3) "107"
[4] => string(3) "107"
[5] => string(3) "108"
[6] => string(3) "109"
}
相反,我得到了这个:
Array (
[0] => 105,105
[1] => 106
[2] => 107,107
[3] => 108
[4] => 109
)
照顾新生儿已经开枪了我的大脑,因为我无法弄清楚我做错了什么。
答案 0 :(得分:1)
$newArray = array();
foreach ($array as $subarray) {
$i = 1; // default value
if ($subarray["f_count"] === 0)
$i = $slipszero;
if ($subarray["f_count"] === 1)
$i = $slipsone;
while ($i--)
$newArray[] = (string)$subarray["uid"];
}
这会将$i
次字符串添加到$i
取决于$subarray["f_count"]
的数组中。
答案 1 :(得分:0)
使用此
$problist = array();
foreach ($rows as $value) {
if ($value['f_count'] == 0) {
for($i=0;$i<$slipszero;$i++)
array_push($problist, $value["uid"];);
} elseif ($value['f_count'] == 1) {
for($i=0;$i<$slipsone;$i++)
array_push($problist, $value["uid"];);
} elseif ($value['f_count'] >= 2) {
$problist[] = $value['uid'];
}
}