Jump to content

Json manipulation/filter with foreach loop using values


zetastreak

Recommended Posts

I have my data array which i converted to json by splitting the keys using for each loop.

Data:

   

$data = Array
    (
    [table6-ios-camt] => 
    [table6-ios-iamt] => 
    [table6-ios-samt] => 
    [table6-ios-type] => 
    [table6-isd-camt] => 0
    [table6-isd-iamt] => 0
    [table6-isd-samt] => 0
    [table6-isd-type] => 
    [table6-ios-csamt] => 
    [table6-isd-csamt] => 0
    [table6-other-camt] => 
    [table6-other-iamt] => 
    [table6-other-samt] => 
    [table6-other-type] => 
    [table6-tran1-camt] => 0
    [table6-tran1-iamt] => 
    [table6-tran1-samt] => 0
    [table6-tran1-type] => 
    [table6-tran2-camt] => 0
    [table6-tran2-iamt] => 
    [table6-tran2-samt] => 0
    [table6-tran2-type] => 
    [table6-itc_3b-csamt] => 0
    [table6-dump_iog-camt] => 
    [table6-dump_iog-iamt] => 
    [table6-dump_iog-samt] => 
    [table6-dump_iog-type] => 
    [table6-dump_oth-camt] => 4386.09
    [table6-dump_oth-iamt] => 
    [table6-dump_oth-samt] => 4386.09
    [table6-dump_oth-type] => 
    [table6-itc_clmd-camt] => 
    [table6-itc_clmd-iamt] => 
    [table6-itc_clmd-samt] => 
    [table6-itc_clmd-type] => 
    [table6-total_10-camt] => 4386.09
    [table6-total_10-iamt] => 0
    [table6-total_10-samt] => 4386.09
    [table6-total_10-type] => 
    [table6-total_11-type] => 
    [table6-total_8-csamt] => 0
    [table6-total_9-csamt] => 0
    [table6-dump_iog-csamt] => 
    [table6-dump_isrc-camt] => 
    [table6-dump_isrc-iamt] => 
    [table6-dump_isrc-samt] => 
    [table6-dump_isrc-type] => 
    [table6-dump_oth-csamt] => 
    [table6-iog_goods-camt] => 
    [table6-iog_goods-iamt] => 
    [table6-iog_goods-samt] => 
    [table6-iog_goods-type] => Capital Goods
    [table6-itc_clmd-csamt] => 
    [table6-total_10-csamt] => 0
    [table6-dump_isrc-csamt] => 
    [table6-iog_goods-csamt] => 
    [table6-iog_inputs-camt] => 
    [table6-iog_inputs-iamt] => 
    [table6-iog_inputs-samt] => 
    [table6-iog_inputs-type] => Input Services
    [table6-iog_inputs-csamt] => 
    [table6-iog_services-camt] => 
    [table6-iog_services-iamt] => 
    [table6-iog_services-samt] => 
    [table6-iog_services-type] => Input Services
    [table6-supp_non_rchrg_goods-camt] => 
    [table6-supp_non_rchrg_goods-iamt] => 
    [table6-supp_non_rchrg_goods-samt] => 
    [table6-supp_non_rchrg_goods-type] => Capital Goods
    [table6-supp_rchrg_reg_goods-camt] => 
    [table6-supp_rchrg_reg_goods-iamt] => 
    [table6-supp_rchrg_reg_goods-samt] => 
    [table6-supp_rchrg_reg_goods-type] => Capital Goods
    [table6-supp_non_rchrg_goods-csamt] => 
    [table6-supp_non_rchrg_inputs-camt] => 4386.09
    [table6-supp_non_rchrg_inputs-iamt] => 
    [table6-supp_non_rchrg_inputs-samt] => 4386.09
    [table6-supp_non_rchrg_inputs-type] => Inputs
    [table6-supp_rchrg_reg_goods-csamt] => 
    [table6-supp_rchrg_reg_inputs-camt] => 
    [table6-supp_rchrg_reg_inputs-iamt] => 
    [table6-supp_rchrg_reg_inputs-samt] => 
    [table6-supp_rchrg_reg_inputs-type] => Input Services
    [table6-supp_non_rchrg_inputs-csamt] => 
    [table6-supp_rchrg_reg_inputs-csamt] => 
    [table6-supp_rchrg_unreg_goods-camt] => 
    [table6-supp_rchrg_unreg_goods-iamt] => 
    [table6-supp_rchrg_unreg_goods-samt] => 
    [table6-supp_rchrg_unreg_goods-type] => Capital Goods
    [table6-supp_non_rchrg_services-camt] => 
    [table6-supp_non_rchrg_services-iamt] => 
    [table6-supp_non_rchrg_services-samt] => 
    [table6-supp_non_rchrg_services-type] => Input Services
    [table6-supp_rchrg_reg_services-camt] => 
    [table6-supp_rchrg_reg_services-iamt] => 
    [table6-supp_rchrg_reg_services-samt] => 
    [table6-supp_rchrg_reg_services-type] => Input Services
    [table6-supp_rchrg_unreg_goods-csamt] => 
    [table6-supp_rchrg_unreg_inputs-camt] => 
    [table6-supp_rchrg_unreg_inputs-iamt] => 
    [table6-supp_rchrg_unreg_inputs-samt] => 
    [table6-supp_rchrg_unreg_inputs-type] => Input Services
    [table6-supp_non_rchrg_services-csamt] => 
    [table6-supp_rchrg_reg_services-csamt] => 
    [table6-supp_rchrg_unreg_inputs-csamt] => 
    [table6-supp_rchrg_unreg_services-camt] => 
    [table6-supp_rchrg_unreg_services-iamt] => 
    [table6-supp_rchrg_unreg_services-samt] => 
    [table6-supp_rchrg_unreg_services-type] => Input Services
    [table6-supp_rchrg_unreg_services-csamt] =>
    );

json:

    {  
       "table6":{  
          "ios":{  
             "camt":0,
             "iamt":0,
             "samt":0,
             "csamt":0
          },
          "isd":{  
             "camt":0,
             "iamt":0,
             "samt":0,
             "csamt":0
          },
          "other":{  
             "camt":0,
             "iamt":0,
             "samt":0,
             "csamt":0
          },
          "tran1":{  
             "camt":0,
             "iamt":0,
             "samt":0,
             "csamt":0
          },
          "tran2":{  
             "camt":0,
             "iamt":0,
             "samt":0,
             "csamt":0
          },
          "itc":{  
             "camt":0,
             "iamt":550,
             "samt":230,
             "csamt":680
          },
      "iog":[  
         {  
            "itc_typ":"cg",
            "camt":0,
            "samt":0,
            "iamt":0,
            "csamt":0
         },
         {  
            "itc_typ":"ip",
            "camt":0,
            "samt":0,
            "iamt":0,
            "csamt":0
         },
         {  
            "itc_typ":"is",
            "camt":0,
            "samt":0,
            "iamt":0,
            "csamt":0
         }
      ],
      "supp_non_rchrg":[  
         {  
            "itc_typ":"cg",
            "camt":0,
            "samt":0,
            "iamt":0,
            "csamt":0
         },
         {  
            "itc_typ":"ip",
            "camt":4386.09,
            "samt":4386.09,
            "iamt":0,
            "csamt":0
         },
         {  
            "itc_typ":"is",
            "camt":0,
            "samt":0,
            "iamt":0,
            "csamt":0
         }
      ],
      "supp_rchrg_reg":[  
         {  
            "itc_typ":"cg",
            "camt":0,
            "samt":0,
            "iamt":0,
            "csamt":0
         },
         {  
            "itc_typ":"ip",
            "camt":0,
            "samt":0,
            "iamt":0,
            "csamt":0
         },
         {  
            "itc_typ":"is",
            "camt":0,
            "samt":0,
            "iamt":0,
            "csamt":0
         }
      ],
      "supp_rchrg_unreg":[  
         {  
            "itc_typ":"cg",
            "camt":0,
            "samt":0,
            "iamt":0,
            "csamt":0
         },
         {  
            "itc_typ":"ip",
            "camt":0,
            "samt":0,
            "iamt":0,
            "csamt":0
         },
         {  
            "itc_typ":"is",
            "camt":0,
            "samt":0,
            "iamt":0,
            "csamt":0
         }
      ]
       }
    }


  
The next thing i want to do is to unset the keys or entire inner objects of the json.

Keeping in mind the following conditions:

  • 1.If all the keys in the objects are 0 then the entire object will be removed (easy part).
  • 2.If a key in the object has any value other than 0 , the object should not be removed and the key with "0" value should also be there. 

i've tried splitting value and using conditions to check for 0 or null values but the problem with that is it removes all keys that have 0 values thus failing condition 2 for the code. 

       $vars = $data;
       $count = "";
       $finalArray = array();
       foreach ($vars as $keys => $values) {
         foreach ($values as $k => $v) {
           foreach ($v as $ke => $ve) {
                   if ($keys == "table6") {
                      $count = (array)$v;    
                      foreach ($count as $ck => $cv) {
                        if ($cv != "0")
                        {
                         $flag = true;
                         break;
                        }
                      }
             
             if($flag == false)
                  {
                    array_push($finalArray, $count);
                  }                         
           }
        }
     }
    print_r($finalArray);
    die();

I expeced the output to contain the objects with key value pairs that does not contain 0 value but everything prints here.

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.