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

5
  • Shouldn't it be $a = array_search(0, array_column($data, 'value')); Commented Aug 22, 2020 at 18:50
  • 2
    json_decode($json_data) returns an object, not array, use json_decode($json_data, TRUE) to get back an array. Commented Aug 22, 2020 at 18:50
  • @AyushGupta yes that line and that from user3783243 fixed it. Many thanks:) Commented Aug 22, 2020 at 19:32
  • @nice_dev how can I select a comment as answer? Should I write it down as answer? Commented Aug 23, 2020 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: 3v4l.org/LFYO1 Maybe you just want to unset($data["Untersuchte Objekte"])? Commented Dec 7, 2020 at 21:24

4 Answers 4

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.

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

Comments

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;
        }
    }
}

5 Comments

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?
@S.LT Depends on what the circumstances are
After $contains = true; I would suggest to break, because if once found, no need to search further.
@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.
In that case, I totally agree. I thought it was only about a detection.
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 :)

3 Comments

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.
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.
hmmm, i see where you wanna go. I will look into that tomorrow. @nice_dev
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.

3 Comments

This will cause error if there are values like +1, -1
@AyushGupta Looking at the OP's dataset, it is unlikely to have -1 count of files.
They are technically all unsigned values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.