php - How to check if an object is null or not? -
i´m trying condition if array nr#1 of xmlhttp.responsetext null. can not work. don´t know what´s wrong..
from php send array using json_encode. , in javascipt use json.parse() methode parse array.
there´re 2 strings in array ["", ""]
and i´m trying detect if first array null alert something.
let´s see code :
var str=xmlhttp.responsetext; var res=json.parse(str); and following lines have tried none of them ´s working !
if (res[0]=null) { alert('hey, it´s null'); } , if (res[0]==null) { alert('hey, it´s null'); } , if (res[0]=="null") { alert('hey, it´s null'); } , if (res[0]=="") { alert('hey, it´s null'); } have used php echo code json , get: ["null",""] so what´s wrong ?
if json data :
["null",""] which implies
$data = array( 0 => "null", 1 => "" ); $json = json_encode($data); echo $json; ---> result yours....["null",""] echo "<br />"; $res = json_decode($json); ----> must decode json first... echo $res[0]; -----> code result "null" wanted... the below statement let know if object empty.
if (res[0]==null) { -----> result "[" not data "null".... alert('hey, it´s null'); } so, firstly must decode json data , next check object.
Comments
Post a Comment