jarvis Posted September 5, 2022 Share Posted September 5, 2022 Hi All, I've got the following code, which creates my JSON feed. It works perfectly: // group data by suite $result = array(); foreach ($bookings as $booking) : $result[$booking['suite']][] = $booking; endforeach; // create our array for the gantt json feed $json = array(); $count = 0; foreach ($result as $itemName => $rows) : $count++; $customClass = ($count % 2 == 1) ? "ganttRed" : "ganttBlue"; foreach ($rows as $row) : $quantity = array_shift($row['quantity']); $data = [[ 'from' => $row["start_date"], 'to' => $row["end_date"], 'label' => $row['person']." (".$quantity.")", 'desc' => $row['name']."<br/>".$row['telephone']."<br/>".$row['email'], 'customClass' => $customClass ]]; $json[] = array( 'name' => $itemName, 'desc' => 'Booking #'.$row["booking_id"], 'values' => $data ); endforeach; endforeach; The only downside, is that the name column outputs each time: [ { "name":"Suite 1", "desc":"Booking #835", "values":[ { "from":"09\/08\/2022", "to":"09\/14\/2022", "label":"Single Room", "desc":"john@smith.com", "customClass":"ganttRed" } ] }, { "name":"Suite 1", "desc":"Booking #833", "values":[ { "from":"09\/06\/2022", "to":"09\/09\/2022", "label":"Ensuite", "desc":"fred@west.com", "customClass":"ganttRed" } ] } ] What I'd like is the following: [ { "name":"Suite 1", "desc":"Booking #835", "values":[ { "from":"09\/08\/2022", "to":"09\/14\/2022", "label":"Single Room", "desc":"john@smith.com", "customClass":"ganttRed" } ] }, { "name":"", "desc":"Booking #833", "values":[ { "from":"09\/06\/2022", "to":"09\/09\/2022", "label":"Ensuite", "desc":"fred@west.com", "customClass":"ganttRed" } ] } ] I wonder how I may go about achieving this please? Quote Link to comment https://forums.phpfreaks.com/topic/315278-php-loop-to-create-json-feed-prevent-duplicate-name-field-as-grouped/ Share on other sites More sharing options...
kicken Posted September 5, 2022 Share Posted September 5, 2022 37 minutes ago, jarvis said: I wonder how I may go about achieving this please? Don't, because that structure makes no sense. If you want one suite entry with several bookings, then structure the data that way so you have a JSON output that looks like this: [ { "name":"Suite 1", "bookings": [ { "desc":"Booking #835", "values":[{ "from":"09\/08\/2022", "to":"09\/14\/2022", "label":"Single Room", "desc":"john@smith.com", "customClass":"ganttRed" }] }, { "desc":"Booking #833", "values":[{ "from":"09\/06\/2022", "to":"09\/09\/2022", "label":"Ensuite", "desc":"fred@west.com", "customClass":"ganttRed" }] } ] } ] There doesn't seem to be much point in your "values" key being an array either. I would remove that extra level and just make values the object. Quote Link to comment https://forums.phpfreaks.com/topic/315278-php-loop-to-create-json-feed-prevent-duplicate-name-field-as-grouped/#findComment-1600115 Share on other sites More sharing options...
jarvis Posted September 5, 2022 Author Share Posted September 5, 2022 Thanks @kicken Sadly, the data has to be that format as it's passed to into creating a gantt chart using a 3rd party tool (a demo can be seen here). Quote Link to comment https://forums.phpfreaks.com/topic/315278-php-loop-to-create-json-feed-prevent-duplicate-name-field-as-grouped/#findComment-1600117 Share on other sites More sharing options...
kicken Posted September 5, 2022 Share Posted September 5, 2022 Then just set $itemName equal to an empty string after you assign to your entry to the $json array. That way, on the first loop, $itemName will contain the site name but on future loops it'll be empty. Quote Link to comment https://forums.phpfreaks.com/topic/315278-php-loop-to-create-json-feed-prevent-duplicate-name-field-as-grouped/#findComment-1600118 Share on other sites More sharing options...
jarvis Posted September 5, 2022 Author Share Posted September 5, 2022 Literally: $json[] = array( 'name' => $itemName, 'desc' => 'Booking #'.$row["booking_id"], 'values' => $data ); $itemName = ''; That works! Thanks, pretty obvious really Quote Link to comment https://forums.phpfreaks.com/topic/315278-php-loop-to-create-json-feed-prevent-duplicate-name-field-as-grouped/#findComment-1600119 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.