Hi i have this data which will fetch from the spreadsheet using blockspring API.My problem is how will i able to foreach this one and display the result? Here is my code below
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;
}
Any help is muchly appriciated. TIA
Using echo $result->Property;
will end up with a notice, because ->
is an object operator (objects), you want to access an array, please try the code below:
foreach ( $res["data"] as $result ) {
echo $result["Property"];
}
Due to the comments I've added a solution for the string 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>";
}