嗨,我有这个数据,它将使用blockspring API从电子表格中获取。我的问题是如何预先处理这个数据并显示结果?这是我在下面的代码
Array
(
[data] => Array
(
[0] => Array
(
[Check In] => Date(2015,10,20)
[Check Out] => Date(2015,10,22)
[Confirmation Number] => 1234567
[Property] => USJ Midas
[Room Number] => 102
[Guest Name] => Greg Happy
[Guest Email] => ghappy@gmail.com
)
)
)
echo "<pre>";
print_r($res);
echo "</pre>";
foreach($res as $result){
echo $result->Property;
}
任何帮助都会被告知。TIA
使用echo $result->Property;
会收到一个通知,因为->
是一个对象运算符(对象),您想访问数组,请尝试下面的代码:
foreach ( $res["data"] as $result ) {
echo $result["Property"];
}
由于这些评论,我为字符串Date(2015,10,20)
:添加了一个解决方案
foreach ( $res["data"] as $result ) {
// pattern /[^0-9,]/ removes everything except numbers and the comma
$checkInString = preg_replace( "/[^0-9,]/" , "", $result["Check In"] );
$checkOutString = preg_replace( "/[^0-9,]/" , "", $result["Check Out"] );
// the result of the regex: (i.e.) 2015,10,20, so we create a DateTime object, from the given format Y,m,d
//
$checkInDate = DateTime::createFromFormat( "Y,m,d", $checkInString );
$checkOutDate = DateTime::createFromFormat( "Y,m,d", $checkOutString );
// call the format method of the DateTime object with the date format we want
echo $checkInDate->format( "m/d/Y" )."<br>";
echo $checkOutDate->format( "m/d/Y" )."<br>";
}