im relativly new to programming (sysadmin) but im banging my head rn. I want to check if a value in an array or json is greather than 0 and set $contains to "true" or "false" depeding on the value of it.
Im creating an array, which im later convert to json:
$fh = fopen("/var/www/path/to/txt/file/". $date . ".txt",'r'); //thats my TXT File
$data = array();
while ($line = fgets($fh)) {
if(trim($line)!=''){
$line_data = explode(':',$line);
$data[]=array('item'=>trim($line_data[0]),'value'=>trim($line_data[1]));
}
}
fclose($fh);
now im remove a specific line from that and convert it to json:
$data = \array_diff_key($data, ["Untersuchte Objekte"]); //removed specific line
$json_data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK); // for further use and convert the string value to int
Now my json looks like:
{
"1": {
"item": "Total items found",
"value": 0
},
"2": {
"item": "Infected objects and other objects",
"value": 0
},
"3": {
"Item": "Disinfected objects",
"value": 0
},
"4": {
"item": "moved to memory",
"value": 0
},
"5": {
"item": "distant objects",
"value": 0
},
"6": {
"item": "uninfected items",
"value": 0
},
"7": {
"item": "examination error",
"value": 0
},
"8": {
"item": "Password protected items",
"value": 0
},
"9": {
"item": "skipped items",
"value": 0
}
}
Now i want that $contains is "true" if one of "value" in the json is > 0. Im trying that with this:
$data = array(json_decode($json_data));
$a = array_search('value', array_column($data, 'value'));
if ($a > 0)
{
$contains = "true";
}
else
{
$contains = "false";
}
How can I achive that, if "value" is > 0, $contains = "true"?