1
foreach($parcels as $parcel):
        $length  = $helper->convertToCM($parcel->length, $parcel->distance->name);
        $width   = $helper->convertToCM($parcel->width, $parcel->distance->name);
        $height  = $helper->convertToCM($parcel->height, $parcel->distance->name);
        $weight  = $helper->convertToKG($parcel->weight, $parcel->mass->name);

        return array(
            "length"        => $length,
            "width"         => $width,
            "height"        => $height,
            "weight"        => $weight,
            "distance_unit" => env('SHIPPO_DISTANCE_UNIT'),
            "mass_unit"     => env('SHIPPO_MASS_UNIT')
        );

result

{
 "length": {},
 "width": {},
 "height": {},
 "weight": {},
 "distance_unit": "cm",
 "mass_unit": "kg"
}

But if I do (inside the foreach)

return $length

I get the expected result... Must be something stupid but I'm not seeing what

2
  • If you return INSIDE a foreach loop it will terminate the foreach on the FIRST ITERATION surprisingly enough Commented Mar 7, 2017 at 19:18
  • That is not the problem, obviously it will stop on the first iteration, thats what I want. The problem is the variable being an object. Commented Mar 7, 2017 at 19:23

1 Answer 1

1

The problem was that the method is returning an object instead of a value, just had json_decode it.

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.