0

我需要将数组中的“日期”保存到 php 变量中。(get_headers) 数组有 2 种类型。

类型1

Array
(
    [0] => HTTP/1.1 302 Object Moved
    [Content-Type] => Array
        (
            [0] => text/html
            [1] => text/html;charset=UTF-8
        )
    [1] => HTTP/1.1 200 OK
    [Date] => Thu, 27 Jun 2013 06:50:37 GMT

类型2

Array
(
    [0] => HTTP/1.1 301 Moved Permanently
    [Date] => Array
        (
            [0] => Thu, 27 Jun 2013 06:50:27 GMT
            [1] => Thu, 27 Jun 2013 06:41:59 GMT
        )

这是我切片的方法:

$arr = array();
$arr = get_headers($url,1);

$date_variable = $arr[Date];

if($date_variable == 'Array'){
        $date = $arr[Date][0];
}
else {
    $date = $arr[Date];
}

我的切片非常难看.. $date_variable == 'Array' 不起作用..

4

2 回答 2

0

这应该这样做:

$date = is_array($arr['Date']) ? $arr['Date'][0] : $arr['Date'];
于 2013-06-27T07:07:19.400 回答
0

这可能对你有帮助

$arr = array();
$arr = get_headers($url,1);

$date_variable = $arr['Date'];

//check the array like this
if(is_array($date_variable)){

    $date = $date_variable[0];
}
else {

    $date = $date_variable;
}
于 2013-06-27T07:15:41.017 回答