How to format this josn array as per requirment

115
March 16, 2018, at 3:56 PM

What i want :

{"success":"true","message":"verified","userid":"1","useremail":"jagdishbolder@gmail.com","products":[{"productId":"3806","productName":"Murgh Tikka Peshawari","status":"Available","primaryimage":"http:\/\/abhidemo.com\/food\/uploads\/primary\/391tandooriChickenTikka2.jpg","description":"Peshawari ChickenKebab Recipe, Learnhow to makPeshawari Chickenm Kebab absolutely delicious recipe ofeshawari ChickenKebab ingredients and cooking method Full of flavour these shallow fried kebabs are great party starters ThisPeshawari Chickenebab recipe is Excellent and find moreGreat recipes","plimit":"1","variants":[{"varientid":"16","variantname":"full","varprice":"100","varquantity":"4","pid":"3806"}]},{"productId":"3806","productName":"Murgh Tikka Peshawari","status":"Available","primaryimage":"http:\/\/abhidemo.com\/food\/uploads\/primary\/391tandooriChickenTikka2.jpg","description":"Peshawari ChickenKebab Recipe, Learnhow to makPeshawari Chickenm Kebab absolutely delicious recipe ofeshawari ChickenKebab ingredients and cooking method Full of flavour these shallow fried kebabs are great party starters ThisPeshawari Chickenebab recipe is Excellent and find moreGreat recipes","plimit":"1","variants":[{"varientid":"17","variantname":"full","varprice":"100","varquantity":"4","pid":"3806"}]}]}

what I have done :i have two diffent table named cart_extra and user_cart where am storing the variants of choose item and then in cart_extra i am storing extra realted to that varient

MY CODE

<?php

            ///active user then check cart of the user ////
            $sqlwish = "SELECT * FROM `users_cart` WHERE `uid`='" . $uid . "'  order by id DESC";
            $checkcart = mysqli_query($conn, $sqlwish);
            $rowcount = mysqli_num_rows($checkcart);
///checking for empty cart///
//echo $rowcount;
            if ($rowcount > 0) {
                ////IF cart is not empty fetching details /////
               while ($cart = mysqli_fetch_array($checkcart)) {
                $variantid[] = $cart['varient_id'];
                $varient_name[] = $cart['varient_name'];
            }
            foreach ($varient_name as $varname) {

                echo '<pre>'.$sqlwish = "SELECT * FROM `users_cart` WHERE `uid`='" . $uid . "' AND varient_name='" . $varname . "' ";
                $checkcart = mysqli_query($conn, $sqlwish);

                while ($cart = mysqli_fetch_array($checkcart)) {
                    $variantid = $cart['varient_id'];
                    $varientquantity = $cart['varient_quantity'];
                    $product_id = $cart['product_id'];
                    $product_name = $cart['product_name'];
                    $varientname = $cart['varient_name'];
                    $varientprice = $cart['varient_price'];
                    $jsonvar[] = array("varid" => $variantid, "varname" => $varientname, "varquantity" => $varientquantity, "varprice" => $varientprice, "product_id" => $product_id, "productname" => $product_name);
                    $qry = "SELECT * FROM app_productsmain where id='" . $product_id . "'";
                    $res1 = mysqli_query($conn, $qry);

                    while ($records1 = mysqli_fetch_array($res1, MYSQLI_ASSOC)) {
                        $product_id = $records1['product_id'];
                        $product_des = $records1['description'];
                        $product_des = htmlspecialchars_decode(str_replace("&quot;", "\"", $product_des));
                        $product_name = $records1['product_name'];
                        $product_status = $records1['product_status'];
                        $product_image_primary = $serverimg . $records1['primary_image'];
                        $product_limit = $records1['plimit'];
                    }
                    $json1[] = array("productId" => $product_id, "productName" => $product_name, "status" => $product_status, "primaryimage" => $product_image_primary, "description" => $product_des, "plimit" => $product_limit, "variants" => $jsonvar);
                 }
            }
                print_r($json1);
                unset($json1);
                } else {
                    $minfo = array("success" => 'true', "message" => 'empty cart');
                    $jsondata = json_encode($minfo);
                    print_r($jsondata);
                    mysqli_close($conn);
                    exit();
                }

but when i loop the table second time i got array having variant both 1st and second

[0] => Array
        (
            [productId] => 3806
            [productName] => Murgh Tikka Peshawari bhui
            [status] => 
            [primaryimage] => 
            [description] => 
            [plimit] => 
            [variants] => Array
                (
                    [0] => Array
                        (
                            [varid] => 17
                            [varname] => full
                            [varquantity] => 4
                            [varprice] => 100
                            [product_id] => 3806
                            [productname] => Murgh Tikka Peshawari bhui
                        )
                )
        )
    [1] => Array
        (
            [productId] => 3806
            [productName] => Murgh Tikka Peshawari bhui
            [status] => 
            [primaryimage] => 
            [description] => 
            [plimit] => 
            [variants] => Array
                (
                    [0] => Array
                        (
                            [varid] => 17
                            [varname] => full
                            [varquantity] => 4
                            [varprice] => 100
                            [product_id] => 3806
                            [productname] => Murgh Tikka Peshawari bhui
                        )
                    [1] => Array
                        (
                            [varid] => 16
                            [varname] => full
                            [varquantity] => 4
                            [varprice] => 100
                            [product_id] => 3806
                            [productname] => Murgh Tikka Peshawari bhui
                        )
                )
        ) 

How can make it corret so every time i loop i got product variant desperately as i want am going to use it as json for my app. thanks in advance.

Rent Charter Buses Company
READ ALSO
i want to formate my google api autocomplete data

i want to formate my google api autocomplete data

i am using google api for address search and i got it completely

161
Which python library would be the most appropriate for accelerometer&#39;s data visualisation? [on hold]

Which python library would be the most appropriate for accelerometer's data visualisation? [on hold]

I have a sensor which contains an accelerometer and a gyroscopeI got those two 3D movements' data via serial port

164
Node.js, RESTIFY, Mongoose, Promise: Trying to $push an array to update but not working

Node.js, RESTIFY, Mongoose, Promise: Trying to $push an array to update but not working

I used this method because I am storing an array of classified messages, I would like to vividly understand why it doesn't update

244
Expose `window` object from imported node module in node.js and electron

Expose `window` object from imported node module in node.js and electron

I'm currently writing a web application that uses a large number of npm packages, many which are designed for browserSome examples are react, typescript, and golden-layout

175