1

is there a better way of doing this ? i need 52 0's at the end of each array I already tryd to make a array and implode it but then it behaves like a string.

for($i = 1; $i <= 52; $i++)
    {       
        array_push($totaal["week".$i], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

    }
Julez
  • 1,184
  • 2
  • 16
  • 23
  • This looks like a code smell. I can't say for sure, as I don't know much about the context of this code, but you're going to end up with a very large data structure that is mostly empty. You may benefit from spending a little time thinking if there's an alternative way to achieve your goals. – Khior Jan 16 '14 at 16:36

2 Answers2

5
$zeros_array = array();
for($i = 1; $i <= 52; $i++)           
   array_push($zeros_array, 0);

for($i = 1; $i <= 52; $i++)       
   $totaal["week".$i] = array_merge($totaal["week".$i], $zeros_array);
zavg
  • 10,351
  • 4
  • 44
  • 67
1

Try putting the zeroes into their own array and then merge it with each in turn. This should be more efficient.

$zeroes = array_fill(0, 52, 0);
for($i = 1; $i <= 52; $i++)
{       
    $totaal["week".$i] = array_merge($totaal["week".$i], $zeroes);
}
Khior
  • 1,244
  • 1
  • 10
  • 20