html - How do I filter php json for matching keys

Solution:

Decode your JSON as array an use array_filter() to filter out elements:

$jsonobj = '{"ABC":35,"DEF":36,"GEH":34}';
$array = json_decode($jsonobj, true);

print_r(array_filter($array, "lessthan"));

function lessthan($var) {
    return $var <= 35;
}

Will output

Array
(
    [ABC] => 35
    [GEH] => 34
)

Edit: if you want to use/keep your $t you can do like so:

<?php

$t = 35;
$jsonobj = '{"ABC":35,"DEF":36,"GEH":34}';
$array = json_decode($jsonobj, true);

print_r(array_filter($array, function ($var) use ($t) {
    return $var <= $t;
}));

Answer

Solution:

If you want to maintain your style of coding then I recommend looping the array and adding to a new array if true.

$t = 35;
$jsonobj = '{"ABC":35,"DEF":36,"GEH":34}';
$obj = json_decode($jsonobj);
$match =[];
foreach($obj as $key => $value){
    if ($t <= $value) {
        $match[] = $key . " => " . $value;
    }
}

if(count($match) > 0){
    echo implode("<br>\n", $match);
}else{
    echo "no matches";
}

See it running here:
https://3v4l.org/NDuD7

Source