Abhishek Mishra Abhishek Mishra - 5 months ago 18
JSON Question

How to Get Valid Json if Using Multiple foreach Loops

$base = 'http://Yourhost';
$proxy = new SoapClient($base.'/api/v2_soap?wsdl=1');
if ($sessionId = $proxy - > login('user123', 'user123')) {
echo "LoginSuccess";
}
$allCategories = $proxy - > catalogCategoryTree($sessionId, 2);
$allCategories = get_object_vars($allCategories);
foreach($allCategories['children'] as $k = > $v {
$v = get_object_vars($v);
echo $cat[] = json_encode(array("Category" = > $v['name']));
// echo 'Category id and name->'.$v['category_id'].$v['name']."</br>";
$subCat = $proxy - > catalogCategoryTree($sessionId, $v['category_id']);
$subCat = get_object_vars($subCat);
foreach($subCat['children'] as $k1 = > $v1) {
$v1 = get_object_vars($v1);
echo $subcat1 = json_encode(array("SubCategory" = > $v1['name']));
//echo $subcat1.",";
//echo 'SubCategory Id and Name ->'.$v1['category_id'].$v1['name']."</br>";
$res = $proxy - > catalogCategoryAssignedProducts($sessionId, $v1['category_id']);
// echo json_encode($res);
//$res=get_object_vars($res);
//$subcat1[]=$v1['category_id'].$v1['name'];
foreach($res as $key = > $value) {
// echo "product"."</br>";
$value = get_object_vars($value);
echo $pdt[] = json_encode(array("Product_ID" = > $value['product_id']));
//$pdt[]=$value['product_id'];
// echo 'Product ID ->'.$value['product_id']."</br>";
}
}
//echo json_encode(array("ProductDemo"=>$res));
}


I am Getting Output Like this-

{
"Category": "Mens Wear"
}{
"Category": "Womens Wear"
}{
"Category": "Mobile Phones"
}{
"Category": "Grocery"
}{
"SubCategory": "SPICES & SEASONING"
}{
"SubCategory": "CEREALS & PULSES"
}{
"SubCategory": "RICE & FLOUR"
}{
"Product_ID": 13
}{
"Product_ID": 24
}{
"SubCategory": "OIL & GHEE"
}{
"SubCategory": "SWEETNER & WHITNER"
}{
"SubCategory": "OTHERS"
}{
"Category": "Food"
}{
"SubCategory": "DRIED READY MEALS"
}{
"SubCategory": "SAUCES,SPREADS & PICKLES"
}{
"SubCategory": "BAKERY"
}{
"SubCategory": "CHIPS & NAMKEENS"
}{
"Category": "BEVERAGES"
}{
"SubCategory": "TEA & COFFEE"
}{
"SubCategory": "HEALTH & ENERGY DRINKS"
}{
"SubCategory": "FRUIT DRINKS"
}{
"Category": "PERSONAL CARE"
}{
"SubCategory": "FACE CARE & LOTION"
}{
"SubCategory": "FACE WASH & SCRUBS"
}{
"SubCategory": "SHAMPOO & CONDITIONER"
}{
"Product_ID": 17
}{
"SubCategory": "ORAL CARE"
}{
"SubCategory": "BODY & HANDWASH"
}{
"SubCategory": "HAIR OIL & SERUM"
}{
"Product_ID": 16
}{
"Product_ID": 21
}{
"SubCategory": "DEO,PERFUMES & TALC"
}{
"SubCategory": "HAIR STYLING CREAMS & GEL"
}{
"SubCategory": "SHAVING NEEDS"
}{
"SubCategory": "SHOE CARE"
}{
"Category": "DETERGENT & TOILETRIES"
}{
"SubCategory": "DETERGENT BAR & POWDER"
}{
"Product_ID": 14
}{
"Product_ID": 15
}{
"Product_ID": 20
}{
"Product_ID": 22
}{
"SubCategory": "SANITARY&FLOOR CLEANERS"
}{
"SubCategory": "FABRIC CARE"
}{
"SubCategory": "DETERGENT BAR & POWDER"
}{
"SubCategory": "DISHWASHER AGENTS"
}{
"Category": "Baby CARE"
}{
"SubCategory": "BABY DIAPERS"
}{
"SubCategory": "BABY CREAMS"
}{
"Product_ID": 18
}{
"SubCategory": "BABY SOAP"
}{
"SubCategory": "BABY SHAMPOO"
}{
"SubCategory": "BABY OIL"
}{
"SubCategory": "BABY FOOD"
}{
"Category": "OTHER PRODUCTS"
}{
"SubCategory": "REPELLENT"
}{
"SubCategory": "MEDICATIONS&TREATMENT"
}{
"Product_ID": 23
}{
"SubCategory": "WOMEN'S HYGIENE"
}{
"SubCategory": "CONDOMS"
}{
"SubCategory": "SEXUAL WELLNESS"
}{
"SubCategory": "FOIL PAPER & NAPKINS"
}


That is not a valid json format.
I am doing like this because i need it in same tree format like
Category->SubCategory->Products
.

Answer

you need formating array in php like this

$result = array(
   'category data' => 'data etc'
   'subcategory' => array(
      'subcategory_data' => array();
   )
);

and you need call json_encode($result) after generate this array.

$allCategories = $proxy - > catalogCategoryTree($sessionId, 2);
$allCategories = get_object_vars($allCategories);
$i = 0;
foreach($allCategories['children'] as $k = > $v {
        $v = get_object_vars($v);
        $cat[$i] = array("Category" => $v['name']);
        $subCat = $proxy - > catalogCategoryTree($sessionId, $v['category_id']);
        $subCat = get_object_vars($subCat);
        $ii = 0;
        foreach($subCat['children'] as $k1 = > $v1) {
            $v1 = get_object_vars($v1);
            echo $cat[$i]["SubCategory"][$ii]['nameCategory'] = > array($v1['name']);
            $res = $proxy - > catalogCategoryAssignedProducts($sessionId, $v1['category_id']);

            $iii = 0;
            foreach($res as $key = > $value) {
                $value = get_object_vars($value);
                 $cat[$i]["SubCategory"][$ii]['product_ids'][$iii] => $value['product_id'];
                $iii++;
            }
            $ii++;
        }
        $i++;
    }
echo json_encode($cat);

but this bad example you need self try generate array and echo by json_encode() this array once.