0
    [
        {
            "Location":"DLI",
            "20FR":"0",
            "20GP":"6",
            "20HC":"0",
            "20HD":"0",
            "20OT":"0",
            ..,,
            ..,,
            "Total":"7",
            "Cost":"251500.000"
        },
        {
            "Location":"LKCMB",
            "20FR":"0",
            "20GP":"0",
            "20HC":"0",
            "20HD":"0",
            "20OT":"0",
            ..,,
            ..,,
            "Total":"9",
            "Cost":"360000.000"
        },
        {
            "Location":"MAA",
            "20FR":"0",
            "20GP":"12",
            "20HC":"0",
            "20HD":"0",
            "20OT":"1",
            ..,,
            ..,,
            "Total":"20",
            "Cost":"1041183.000"
        }
    etc.,
    ]

i have json output as given above, now i need to filter this json based on key value. for example in this above json 20FR is 0 in each array so i need to filter it , 20GP is not having 0 in all array so no need to filter this one, again 20HC and 20HD are 0 in all and have to filter again 20OT is not 0 in one array so no need to filter like i have to filter the json. please someone help me. this is what expecting output.

    [
        {
            "Location":"DLI",
            "20GP":"6",
            "20OT":"0",
            ..,,
            ..,,
            "Total":"7",
            "Cost":"251500.000"
        },
        {
            "Location":"LKCMB",
            "20GP":"0",
            "20OT":"0",
            ..,,
            ..,,
            "Total":"9",
            "Cost":"360000.000"
        },
        {
            "Location":"MAA",
            "20GP":"12",
            "20OT":"1",
            ..,,
            ..,,
            "Total":"20",
            "Cost":"1041183.000"
        }
    etc.,
    ]

please someone help me how to do this in php or jquery or javascript. Thank you

4
  • The above json format is invalid. Commented Jul 15, 2015 at 12:19
  • i think i would filter it in PHP (but thats just personal preference) use json_decode to get it back to a array, then loop through it with a foreach and remove all your 0 values..... when your loop is finished change back the array to a json object with json_encode Commented Jul 15, 2015 at 12:32
  • there are array filtering functions in both languages that are not hard to find with a simple search Commented Jul 15, 2015 at 12:33
  • Do you mean you have an array of objects and you want to create array from those objects. In the new objects you want to filter out all the keys that begin with "20" and have value of "0" in all the objects? Commented Jul 15, 2015 at 13:12

1 Answer 1

1

You could do something like this. I'm not sure this is the most efficient way but it gets the job done. I made a function which checks your json.

<?php

$oldJson = '[
        {
            "Location":"DLI",
            "20FR":"0",
            "20GP":"6",
            "20HC":"0",
            "20HD":"0",
            "20OT":"0",
            "Total":"7",
            "Cost":"251500.000"
        },
        {
            "Location":"LKCMB",
            "20FR":"0",
            "20GP":"0",
            "20HC":"0",
            "20HD":"0",
            "20OT":"0",
            "Total":"9",
            "Cost":"360000.000"
        },
        {
            "Location":"MAA",
            "20FR":"0",
            "20GP":"12",
            "20HC":"0",
            "20HD":"0",
            "20OT":"1",
            "Total":"20",
            "Cost":"1041183.000"
        }
    ]';

function filterJson($json){

    //Decodes a JSON string, When TRUE, it will be converted into associative arrays. 
    $array = json_decode($json, true);

    //count number of inner arrays
    $nbr = count($array);

    //get all keys of the inner array
    $keys = array_keys($array[0]);

    //iterate through the keys
    foreach($keys as $key){

        //check every inner array per key
        for($x=0; $x<$nbr; $x++){

            //if not 0 than break
            if($array[$x][$key] != "0"){
                break;
            }

            //if we didn't break before the last array, all values are 0 and we can unset those values.     
            if($x == $nbr-1){

                //iterate through the arrays 
                for($x=0; $x<$nbr; $x++){
                    unset($array[$x][$key]);
                }

            }
        }
    }

    $json = json_encode($array);
    return $json;
}

$newJson = filterJson($oldJson);

echo $newJson;

?>
Sign up to request clarification or add additional context in comments.

Comments

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.