dqq3623 2016-02-08 10:35
浏览 36
已采纳

嵌套数组中的访问元素PHP - 数组到字符串转换错误

I need have an array structured like so

Array
 (
    [id] => 
    [name] => 
    [description] => 
    [table_id] => 
    [records] => Array
        (
            [0] => Array
                (
                    [id] => 
                    [table_id] => 
                    [created] => 2016-01-15T09:19:36-0700
                    [updated] => 
                    [created_by] => 
                    [updated_by] => 
                    [fields] => Array
                        (
                            [created] => 
                            [Delivery Date] => 
                            [Total Price Including Shipping] =>
                            [Enquiry Status] => 
                            [Equipment Booked 1] =>
                            [Equipment Booked 2] => 
                            [Equipment Booked 3] => 
                            [Equipment Booked 4] => 
                            [Equipment Booked 5] =>
                            [Equipment Booked 6] =>
                            [Surname] => 
                        )

                )

        )

    [record_count] => 1
)

I need to extract the [created] field from the last element of the nested array in [records]. I've tried doing the following

$target_elems = $target.['record_count'];
//print_r($target_elems);

//echo $target_elems;
//echo $target.['records'].[$target_elems].['created'];
echo '<pre>'; print_r($target); echo '<pre/>';

bur I always get Array to String conversion error (commented lines have been tested obviously).

the only generated output is

ArrayArrayArrayArray
  • 写回答

3条回答 默认 最新

  • dtq81142 2016-02-08 10:43
    关注

    As you said:- I need to extract the [created] field from the last element of the nested array in [records].

    first count the number of records in records sub-array and then get the data:-

    <?php
      $count = count($target['records']); // count number of record in records sub-array
      echo $target['records'][$count-1]['created']; // get last created from records array
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?