Ignore JSON element with PHP

Asked

Viewed 50 times

0

Let’s say we have a JSON with 2 elements to follow:

    exemplo: { 
    [{
    "arquivo": "Arquio 1",
    "size": "2340"
    }, {
    "arquivo": "Arquivo 2",
    "size": "0"
     }, {
     "arquivo": "arquivo 3",
     "size": "4329"
     }

Use PHP for() to make a list of files.

For example:

    for($i=0; $i<=10; $i++)
    {
    $arquivo = $json->exemplo[$i]->arquivo;
    $size = $json->exemplo[$i]->size;

    echo "Nome: ".$nome." - ".$size."<br />";
    }

What I want to do is ignore files from the list that are in size(size) "0" and leave only those larger than 0 before going to for() PHP.

Can someone help me?

1 answer

1


That should work:

for($i=0; $i<=10; $i++)
{
  $arquivo = $json->exemplo[$i]->arquivo;
  $size = $json->exemplo[$i]->size ;

  if ((int)$json->exemplo[$i]->size < 1) {
      unset($json->exemplo[$i]);

  } else {
      echo "Nome: ".$nome." - ".$size."<br />";
  }
}
  • Thank you for your reply! Let’s say: if you have 3 results and these 3 are with size 0, then inside the for() will be blank. Can you change this so it doesn’t go blank? or return an error msg if all results are size 0

  • It is, if all elements are invalid, the array is empty. What you want to occur in this case?

  • Here’s an example: http://goo.gl/1iVZBg see that everything is empty when all is empty, and like if everything is empty, you can return an error msg?

  • You have yes, after for: if (Count($json->example)=0) ː echo "No file"; }

  • Thank you! I’ll test.

  • It helped.. Thank you!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.