Jump to content

Multidimensional arrays and JSON validation


terrid

Recommended Posts

Hi all

 

I have an array and within that array that list categories. Each category has a sub category.

I am trying to output in JSON and my problem is this:

 

I am looping through each category and displaying the results. As the category has sub categories I am then looping through them and displaying the sub categories.

This works fine, but my JSON is not valid because of a single comma:

 

{
   "config":{
      "logo":"uploads\/global_config\/scaled_",
      "deliveryCost":19.99,
      "showPrice":"true"
   }
},
"range":[
   {
      "title":"Classic",

      "doors":[
         {
            "id":19,
            "title":"Rembrandt"
         },
         {
            "id":66,
            "title":"Picasso"
         },
         {
            "title":"Heritage",
            "doors":[
               {
                  "id":29,
                  "title":"Broadway"
               },
               {
                  "id":30,
                  "title":"Draycott"

               },
  
               {
                  "title":"Regency",

                  "doors":[
                     {
                        "id":36,
                        "title":"Canterbury"
                     },

                     {
                        "id":47,
                        "title":"Exeter"
                     },      <---- this comma is failing my json

                  ],
                  "product":{
                     "glass":[
                        {
                           "id":7,
                           "title":"Pad"
                        },
                        {
                           "id":6,
                           "title":"Scroll"
                        }
                     ]
                  }
               }

My PHP:

 

<?php
    echo '{';
        $count = count($config) - 1;

        foreach($config as $key=>$conf)
        {
             $conf_price = $conf->getPrice() == '1';
                if($conf_price)
                     $conf_p = 'true';  else  $conf_p = 'false';
                        echo "\"config\"".":","";
                            $config_array = array('logo'=>strtolower(str_replace(' ','-','uploads/global_config/scaled_'.$conf->getImage1())), 'deliveryCost'=>$conf->getDeliveryCost(), 'showPrice'=>$conf_p);
                                if($key == $count){
                                    echo json_encode($config_array).","."\n";
                                } else {
                                    echo json_encode($config_array).'},'."\n";
                                }

       }
          echo json_encode("range").": [\n";

              $last_key = end(array_keys($categories));

              foreach ($categories as $key => $cat)
              {
                if ($key == $last_key)
                {
                    echo "{".   "\"title\"".':'.'"'.$cat->getTitle().'"'.","."\"src\"".':'.'"'."-".strtolower(str_replace('.png','*.png',(str_replace(' ', '-', 'uploads/ranges/scaled_'.$cat->getImage1())))).'"' .","."\"info\"".':'.'"'.strip_tags($cat->getDescription()).'"'.","."\n";
                } else {
                    echo "{".   "\"title\"".':'.'"'.$cat->getTitle().'"'.","."\"src\"".':'.'"'."-".strtolower(str_replace('.png','*.png',(str_replace(' ', '-', 'uploads/ranges/scaled_'.$cat->getImage1())))).'"' .","."\"info\"".':'.'"'.strip_tags($cat->getDescription()).'"'.","."\n";
                }

                            echo json_encode("doors").": [\n";

                                $count = count($cat->getAllDoorStyles()) - 1;
                                foreach ($cat->getAllDoorStyles() as $key=>$door)
                                {
                                     $array2 = array('id'=>$door->getId(), 'title'=>$door->getTitle(), 'src'=>strtolower(str_replace('.png','*.png',(str_replace(' ', '-', 'uploads/doors/scaled_'.$door->getImage1())))), 'price'=>$door->getPrice(), 'description'=>strip_tags($door->getDescription()));
                                     if($key == $count)
                                     {
                                         echo json_encode($array2).''."\n"."".",";
                                     } else {
                                            echo json_encode($array2).''."\n".""."";
                                     }
                                }
                        }
                   echo "\n";
                   echo "],"."\"Product:\"". '{ ';

                   echo json_encode("glass").": [\n";

                     $count = count($glass) - 1;
                     foreach($glass as $key => $glass_cat)
                     {
                     $glass_array = array('id'=>$glass_cat->getId(), 'title'=>$glass_cat->getTitle(), 'price'=>$glass_cat->getPrice(),'src'=>strtolower(str_replace('.png','*.png',(str_replace(' ', '-', 'uploads/glass_option/scaled_'.$glass_cat->getImage1())))));
                     if($key == $count)
                     {
                            echo json_encode($glass_array)."\n";
                     } else {
                            echo json_encode($glass_array).','."\n";
                     }
                    }
                    echo ']';

                             echo '}';
                             echo '}';

 

I need to omit the comma that is causing me my problem

 

All help welcomed

 

Link to comment
Share on other sites

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.