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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.