tooly228
tooly228

Reputation: 15

PHP doesn't echo this range properly

Here is my code:

<?php
$myarray = range(49500,49600);
foreach ($myarray as $key => $item) {
    echo "paper_crumbs[" .$key . "] = {type: HEAD, cost: 0, is_member: false}; <br />";
}
?>

Instead of echoing:

paper_crumbs[49500] = {type: HEAD, cost: 0, is_member: false}; 
paper_crumbs[49501] = {type: HEAD, cost: 0, is_member: false}; 

...etc...

with values all the way to 49,600 as expected, it goes from 0 to 100 like:

paper_crumbs[0] = {type: HEAD, cost: 0, is_member: false}; 
paper_crumbs[1] = {type: HEAD, cost: 0, is_member: false}; 

Many thanks in advance for assistance! This is my first time posting here.

Upvotes: 0

Views: 57

Answers (3)

michaelhagedon
michaelhagedon

Reputation: 40

Change echo "paper_crumbs[" .$key . "] to echo "paper_crumbs[" .$item . "]. The range is in the array values, not the keys.

Upvotes: 0

Salmon
Salmon

Reputation: 705

Instead of "paper_crumbs[" .$key . "] try "paper_crumbs[" .$item . "]

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324760

Erm... you are echoing using the keys, and expect the values to just magically show up out of nowhere?

foreach(range(49500,49600) as $i) echo "paper_crumbs[".$i."] = ...<br />";

Upvotes: 2

Related Questions