Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find whether a json array is empty or not?

Tags:

json

arrays

php

How to find whether a json array is empty or not using PHP? empty($jsonarray) seems doesn't work!

like image 790
hbase_user Avatar asked Apr 02 '11 07:04

hbase_user


2 Answers

Assuming you have decoded the JSON, yes it does.

<?php
    $json = '{"hello": ["world"], "goodbye": []}';
    $decoded = json_decode($json);
    print "Is hello empty? " . empty($decoded->{'hello'});
    print "\n";
    print "Is goodbye empty? " . empty($decoded->{'world'});
    print "\n";
?>

gives:

Is hello empty?
Is goodbye empty? 1

like image 93
Quentin Avatar answered Nov 08 '22 09:11

Quentin


Try this

if(count(json_decode($jsonarray,1))==0) {
    echo "empty";
}

//or
if(empty(json_decode($jsonarray,1))) {
    echo "empty";
}
like image 27
Starx Avatar answered Nov 08 '22 07:11

Starx