0

This is my JSON.
Stored in variable named $onceBooking.

I want to get booking_slots from each booking.

This is my code:

for ($i = 0; $i < count($onceBooking); $i++) {
    if (count($onceBooking[$i]->bookings) == 0) {
        $onceBooking[$i]->total_days = 0;
    } else {
        $onceBooking[$i]->total_days = count($onceBooking[$i]->bookings);
        $bookings = $onceBooking[$i]->bookings;
        for ($j = 0; $j < count($bookings); $j++) {
            return $bookings[$j]->booking_slots;
        }
    }
}

But it's returning nothing. Please tell me if I am wrong.
Thanks.

1
  • Have you decoded the JSON response in your $onceBooking array ? If not, use $onceBooking = json_decode($onceBooking, true); and then check the loop. Commented Jun 14, 2018 at 18:54

2 Answers 2

1

You have to modify the code a little bit because you are returning the first element's booking slot.

$data = json_decode($onceBooking);
$bookingSlots = array();
for ($i = 0; $i < count($onceBooking); $i++) {
    if (count($onceBooking[$i]->bookings) == 0) {
        $onceBooking[$i]->total_days = 0;
    } else {
        $onceBooking[$i]->total_days = count($onceBooking[$i]->bookings);
        $bookings = $onceBooking[$i]->bookings;
        for ($j = 0; $j < count($bookings); $j++) {
            if (!empty($bookings[$j])) {
                $bookingSlots[$bookings[$j]->id] = $bookings[$j]->booking_slots;
            }
        }
    }
}
return $bookingSlots;

Hope this helps.

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

3 Comments

Ok. Glad I could help.
that is because true keyword is used in json_decode function to convert the response as Associative Arrays. As in your code, you are accessing the Objects that is created if true is not used. If true is used then you'll have to access them in array format.
Sure. No Problem. Glad I could help.
1
$data = json_decode($onceBooking);

Then you can loop through data to grab your booking objects.

$result = ["total_days" => 0];

foreach($data as $instance) {
    $result["total_days"] += count($instance->bookings);
    $bookings = $instance->bookings;
    for($i = 0; $i < count($bookings); $i++) {
        return $bookings->booking_slots;
    }
}

-- EDIT: for returning total days & bookings.

$result = ["total_days" => 0, "bookings" => []];

foreach($data as $instance) {
    $result["total_days"] += count($instance->bookings);
    array_push($result["bookings"], $instance->$bookings);
}

return $result;

5 Comments

the return statement seems odd, feel like you're not even using the total_days variable then.
Actually, i want to calculate total days of booking that's why i am using total _days.
if bookings contains no slots then add total_days as 0 otherwise get the slots and count there unique days.
Added an edit to return the bookings for you to calculate in your calling method.
thanks but i want to count unique booking_slots as total_days if booking is not null

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.