dongxie3963 2015-11-19 12:16
浏览 7
已采纳

使用PHP在blockspring电子表格中显示数组中的数据

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

  • 写回答

1条回答 默认 最新

  • dpgkg42484 2015-11-19 12:24
    关注

    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>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等