1

Having some trouble getting a valid JSON output from a php for loop, here is my JSON:

[{"title":"One Colour ($2.45)","price":"($2.45)"},{"title":"Two Colours ($3.35)","price":"($3.35)"},{"title":"Three Colours ($4.25)","price":"($4.25)"}],[{"title":"One Colour ($2.45)","price":"($2.45)"},{"title":"Two Colours ($3.35)","price":"($3.35)"},{"title":"Three Colours ($4.25)","price":"($4.25)"},{"title":"One Colour ($3.05)","price":"($3.05)"},{"title":"Two Colours ($4.35)","price":"($4.35)"},{"title":"Three Colours ($5.75)","price":"($5.75)"}], 

And here is my php loop that creates the json output

foreach ( $product_addons as $addon ) {


    foreach ( $addon['options'] as $option ) :

        $loop ++;
        switch ($qty) {
            case ($qty < 20):
                $price = $option['price'] > 0 ? ' (' . wc_price( get_product_addon_price_for_display( $option['price'] ) ) . ')' : '';
                $title = strip_tags($option['label']. $price);


            break;
            case ($qty > 20 && $qty < 35):
                $price = $option['discount'] > 0 ? ' (' . wc_price( get_product_addon_price_for_display( $option['discount'] ) ) . ')' : '';
                $title = strip_tags($option['label']. $price);
            break;


        }

        $select_text[] = array( 
                'title' => trim($title),
                'price' => trim(strip_tags($price)),
        );



    endforeach;
    echo json_encode($select_text).",";
}

The problem I am getting now is that the JSON output is now valid and I cant quite figure out how to improve it.

rhysclay
  • 1,645
  • 3
  • 22
  • 42

2 Answers2

1

The problem according to https://jsonformatter.curiousconcept.com/ is

Multiple JSON root elements

The solution is puttin everything in 1 embedding aray:

[  
   [  
      {  
         "title":"One Colour ($2.45)",
         "price":"($2.45)"
      },
      {  
         "title":"Two Colours ($3.35)",
         "price":"($3.35)"
      },
      {  
         "title":"Three Colours ($4.25)",
         "price":"($4.25)"
      }
   ],
   [  
      {  
         "title":"One Colour ($2.45)",
         "price":"($2.45)"
      },
      {  
         "title":"Two Colours ($3.35)",
         "price":"($3.35)"
      },
      {  
         "title":"Three Colours ($4.25)",
         "price":"($4.25)"
      },
      {  
         "title":"One Colour ($3.05)",
         "price":"($3.05)"
      },
      {  
         "title":"Two Colours ($4.35)",
         "price":"($4.35)"
      },
      {  
         "title":"Three Colours ($5.75)",
         "price":"($5.75)"
      }
   ]
]

and 2: on line echo json_encode($select_text).","; why would you add a trailing comma?

online Thomas
  • 8,864
  • 6
  • 44
  • 85
0

Ended up figuring it out by looking at this thread: create multidimensional array using a foreach loop

Basically all I needed to do was to use the index of the first foreach loop in a multidimensional array. Here is my final code:

foreach ( $product_addons as $key => $addon ) {

    foreach ( $addon['options'] as $option ) :

        $loop ++;
        switch ($qty) {
            case ($qty < 20):
                $price = $option['price'] > 0 ? ' (' . wc_price( get_product_addon_price_for_display( $option['price'] ) ) . ')' : '';
                $title = strip_tags($option['label']. $price);

            break;
            case ($qty > 20 && $qty < 35):
                $price = $option['discount'] > 0 ? ' (' . wc_price( get_product_addon_price_for_display( $option['discount'] ) ) . ')' : '';
                $title = strip_tags($option['label']. $price);
            break;

        }

        $title = trim($title);

         // Add in your custom fields or WP fields that you want
         $locations[$key][] = array(
           'title' => $title,

         );

    endforeach; 
}
echo json_encode($locations);
Community
  • 1
  • 1
rhysclay
  • 1,645
  • 3
  • 22
  • 42