0

我正在尝试创建一个数组,其中的值根据一些变量重复。在这种情况下:$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
)

照顾新生儿让我大吃一惊,因为我不知道自己做错了什么。

4

2 回答 2

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"]

于 2013-04-23T17:21:21.207 回答
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'];
        }
    }
于 2013-04-23T17:24:07.277 回答