1

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"?

Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92
Simon Müller
  • 211
  • 2
  • 7
  • Shouldn't it be `$a = array_search(0, array_column($data, 'value'));` – Ayush Gupta Aug 22 '20 at 18:50
  • 2
    `json_decode($json_data)` returns an object, not array, use `json_decode($json_data, TRUE)` to get back an array. – user3783243 Aug 22 '20 at 18:50
  • @AyushGupta yes that line and that from user3783243 fixed it. Many thanks:) – Simon Müller Aug 22 '20 at 19:32
  • @nice_dev how can I select a comment as answer? Should I write it down as answer? – Simon Müller Aug 23 '20 at 18:26
  • Be aware that your `intersect_diff_key()` does not work as intended because you are asking to compare keys but your 2nd argument has a key of `0` not `Untersuchte Objekte`. See it fail here: https://3v4l.org/LFYO1 Maybe you just want to `unset($data["Untersuchte Objekte"])`? – mickmackusa Dec 07 '20 at 21:24

4 Answers4

2

You can iterate on your array and stop it when you have what you search:

$contains = false;
foreach(json_decode($json_data, true) as $item){
    if ($item['value']) {
        $contains = true;
        break;
    }
}

You can achieve that in many ways. But that can help you to go further.

SwissLoop
  • 466
  • 2
  • 11
1

It would be quicker to do this when first create the data JSON data (so when reading the .txt file)....

$contains = false;
while ($line = fgets($fh)) {
    if(trim($line)!=''){
        $line_data = explode(':',$line);
        $data[]=array('item'=>trim($line_data[0]),'value'=>trim($line_data[1]));
        if ($line_data[1] > 0)  {
            $contains = true;
        }
    }
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • That's maybe not very "separation of concerns" friendly. You can use a service to read your file and another service to process your data, no? What do you think? – SwissLoop Aug 22 '20 at 18:58
  • @S.LT Depends on what the circumstances are – Nigel Ren Aug 22 '20 at 19:01
  • After `$contains = true;` I would suggest to `break`, because if once found, no need to search further. – Markus Zeller Aug 23 '20 at 07:52
  • @MarkusZeller, as this is loading the data, using break will stop the data load. If the only purpose of the data load is to test this value then it would be good, if not then it will miss the rest of the data. – Nigel Ren Aug 23 '20 at 07:59
  • In that case, I totally agree. I thought it was only about a detection. – Markus Zeller Aug 23 '20 at 09:15
0

You can use array_sum to sum all elements of the array. If you get a positive sum, then there are elements greater than 0.

$values = array_column($data,'value');
$contains = array_sum($values) > 0;

Note: This will always work assuming your value will never go below zero.

nice_dev
  • 17,053
  • 2
  • 21
  • 35
0

So for the answer wich solved my problem, @AyushGupta and @user3783243 delivered in a comment, on my post, the right part.

Changing

$data = array(json_decode($json_data));
$a = array_search('value', array_column($data, 'value'));
if ($a > 0)
  {
    $contains = "true";
  }
else
  {
    $contains = "false";
  }

to

$data = array(json_decode($json_data, TRUE));
$a = array_search(0, array_column($data, 'value'));
if ($a > 0)
  {
    $contains = "true";
  }
else
  {
    $contains = "false";
  }

delivered the solution so $contains is now "true" or "false". Thanks to all :)

Simon Müller
  • 211
  • 2
  • 7
  • 1
    This totally incorrect. Let me tell you it's not working in your case. It might be returning array index which has 0 and the index might have been greater than 0. – nice_dev Aug 23 '20 at 20:00
  • 1
    array_search is searching for value 0 but you wanted to check if any value is greater than 0. These are 2 totally different things. – nice_dev Aug 23 '20 at 20:02
  • 1
    hmmm, i see where you wanna go. I will look into that tomorrow. @nice_dev – Simon Müller Aug 24 '20 at 21:36