Jump to content

PHP Loop to create JSON feed, prevent duplicate name field as grouped


Recommended Posts

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?

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.