0

I am trying to create an array with values repeated according to some variables. In this scenario: $slipszero = "2" & $slipsone = "1", and those values can change.
This is my code as of now:

$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'];
        }
    }

So, starting with this array:

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 ) 
)  

I'd like to end up with this array:

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

Instead, I'm getting this:

Array ( 
[0] => 105,105 
[1] => 106 
[2] => 107,107 
[3] => 108
[4] => 109
)

Taking care of a newborn has shot my brain, because I cannot figure out what I'm doing wrong.

David
  • 1,175
  • 1
  • 16
  • 29

2 Answers2

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

This is adding $i times the string to the array where $i is depending on $subarray["f_count"].

bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • For efficiency sake, I was trying to use `push_array()` rather than `$array[] =` whenever inserting more than one row. – David Apr 23 '13 at 17:46
  • @David `array_push` is not more efficient when you use only one single argument. It's slower than `$array[]`. (And here you only can push the values one per one) – bwoebi Apr 23 '13 at 17:48
  • Right, which is why I specified "whenever inserting more than one row." – David Apr 23 '13 at 18:18
  • @David "And here (in this specific case) you only can push the values one per one" I just said. There exists ways with `call_user_func_array` if you really want to, but this is even more inefficient. – bwoebi Apr 23 '13 at 18:20
  • OK, I now see what you meant by that. One issue though: `===` ought to be `==`. Thank you very much. – David Apr 23 '13 at 18:40
0

use this

$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'];
        }
    }
Ali Akbar Azizi
  • 3,272
  • 3
  • 25
  • 44