0

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.

2 Answers 2

1

I think the problem is that you are trying to deal with values at different levels in the same way, ios has a set of numeric values and so needs to be processed directly, but iog etc has a nested set of arrays. I noticed that in your inner array you seem to be checking the data from one of the higher level loops rather than the current one...

   foreach ($v as $ke => $ve) {
       if ($keys == "table6") {
           $count = (array)$v;  

Your loop value is $ve and yet you are checking $v.

Although this isn't probably as elegant as it could be to handle this, it basically will find all of the relevant items it needs to check and the uses array_sum() to add up all of the values, if the result is > 0, then it copies th data (with the appropriate structure) across...

$vars = $data;
$finalArray = array();
foreach ($vars as $keys => $values) {
    foreach ($values as $k => $v) {
        // Check if this also has sub-arrays
        if ( is_array($v[array_keys($v)[0]] ))   {
            foreach ($v as $k1 => $v1) {
                if ( array_sum($v1) > 0 )    {
                    $finalArray[$keys][$k][$k1] = $v1;
                }
            }
        }
        if ( array_sum($v) > 0 )    {
            $finalArray[$keys][$k] = $v;
        }
    }
}
print_r($finalArray);
Sign up to request clarification or add additional context in comments.

1 Comment

Your approach works like a charm! thanks for the inputs and insights! i never considered using array_sum(); initially i tried using for loop and incremented the counter to achive the same but failed. Thanks :)
1

I think recursive approach is better here - as:

function removeAllZero(&$arr) {
    foreach($arr as $k => &$e) {
        if (is_array($e) && removeAllZero($e)) // if array goes and all zero
            unset($arr[$k]); // remove it 
        else if ($e != 0) return false; // there are non-zero 
    } 
    return true; 
}

Notice the & to change the original array

Live example 3v4l

1 Comment

Thanks for the inputs. Your Approach is very helpful but for some odd reason it's not working on dynamic data sets (i guess i'm having issues with json manipulation that's why! but on static data it's pretty cool. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.