使用PHP的JSON foreach [关闭]

Ok as per your suggestion I updated... By default Laravel returns JSON... I have set it to return an array but I am still getting the same row duplicated using:

$limits = array();
    foreach($pieces as $coverage_limit){
            $limits[] = coveragelimit::index($coverage_limit);
        }
    return json_encode($limits);
    }

the $limits array will hold the last value what coveragelimit::index() returns over the iterate, I would suggest a check on coveragelimit::index() return value if it falls with "Marc B"'s answer.

EDIT:

foreach($pieces as $key=>$coverage_limit) {
    $limits[$key] = coveragelimit::index($coverage_limit);
}

or

foreach($pieces as $coverage_limit) {
    array_push($limits, coveragelimit::index($coverage_limit));
}

both should returns the same as Marc B's answer

You're just overwriting $limits inside that foreach() loop. Perhaps you mean something more like

foreach($pieces as $coverage_limit){        
    $limits[] = coveragelimit::index($coverage_limit);
           ^^--- array push?
}

As well, you don't "implement" JSON instead of arrays. You work with NATIVE data structures, then encode that structure into JSON. JSON's a transport format, it's not something you should ever deal with natively.