2012-03-30 65 views
1

我有一个php函数,它返回一个包含数字或日期字符串对象的数组,我需要知道如何找出返回的是一个日期字符串对象,下面是一个例子代码的检查变量数据是否返回日期对象

PHP函数

function dateTimerArr($timestamp){ 
    //only return date array if days is less than or equal to 31 
    if(date("z", $timestamp)<=31){ 
     return array(
     's'=>intval(date("s", $timestamp)), //seconds 
     'i'=>intval(date("i", $timestamp)),  //min 
     'H'=>intval(date("H", $timestamp)),  //hours 
     'z'=>intval(date("z", $timestamp))  //days 
    ); 
    } 

    //otherwise return date 
    else{ 
     return date('d M Y | h:i A', $timestamp); 
    } 
} 

jquery的代码的一部分

success: function(data){ 
    if(/*if data is date string object*/){ 
     alert('date string object'); 
    }else{ 
     alert('in this case an array has been returned') 
    } 
} 
+0

这个结果是如何被发送到客户端的? JSON编码?然后'$ .isArray(data)'就是你想要的。 – Niko 2012-03-30 17:17:26

+0

是的,它是JSON编码,但我试过'$ .isArray(data);'因为某种原因它返回false – 2012-03-30 17:18:49

+0

它应该返回false,如果数据是一个数组。你可以用Firebug来检查ajax调用,以确保期望的数据得到传输吗? – Niko 2012-03-30 17:20:34

回答

1

使用jQuery的isPlainObject:

if (!$.isPlainObject(data)) { 
    alert('date string object'); 
} else { 
    alert('in this case an array has been returned') 
} 
+0

感谢'$ .isPlainObject'似乎工作 – 2012-03-30 17:29:56

相关问题