0

[Solved]

I want create array consist 40 elements (10x values "c", 10x values "d", 10x values "h", 10x values "s"). I try create array:

$znaczki = array_fill(0, $iloscZnaczkow, 'c');
$znaczki = array_fill($iloscZnaczkow, $iloscZnaczkow, 'd');
$znaczki = array_fill((2 * $iloscZnaczkow), $iloscZnaczkow, 'h');
$znaczki = array_fill((3 * $iloscZnaczkow), $iloscZnaczkow, 's');

Var $IloscZnaczkow contains number - 10. Unfortunetlly, later I will use $znaczki and script

$i = 0;
foreach ($serializ as $key => $value) {
    echo '<img src="images/' . $value . $znaczki[$i] . '.gif" />';
    $i++;
}

throw image src="images/11.gif" (it doesn't have $znaczki[$i] everywhere :()

7
  • where is $serializ coming from ? Commented Feb 9, 2014 at 15:02
  • What does $serializ contains? Commented Feb 9, 2014 at 15:04
  • Also, pls make var_dump of $znaczki after array filling. Commented Feb 9, 2014 at 15:06
  • I mean where has OP defined this variable ? AS far as I'm concerned this variable could be an array as much as it could be a cheese pizza... Can OP please var_dump() all variables ? Commented Feb 9, 2014 at 15:06
  • mmmmm, cheese pizza with array filling. Commented Feb 9, 2014 at 15:08

1 Answer 1

5

The problem is you keep overwriting the array:

$znaczki = array_merge(
    array_fill(0, $iloscZnaczkow, 'c'),
    array_fill(0, $iloscZnaczkow, 'd'),
    array_fill(0, $iloscZnaczkow, 'h'),
    array_fill(0, $iloscZnaczkow, 's')
);

is what you want. You don't really need the index math anymore, as array_merge will maintain the indices for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.