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


access element in nested array PHP - array to string conversion error

我需要有一个这样的数组

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
)

我需要从 [记录] 中嵌套数组的最后一个元素中提取 [created] 字段。我尝试执行以下操作

$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 我总是得到数组到字符串的转换错误(注释行显然已经过测试)。

唯一生成的输出是

ArrayArrayArrayArray

正如你所说:- I need to extract the [created] field from the last element of the nested array in [records].

首先计算子数组records记录数,然后获取数据:

<?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
?>

您可以通过以下方式获取created条记录的字段值。

$last_record = end($target['records']); // get the last record of an array
echo $last_record['created']; // print value of array key 'created'

希望它能帮助你:)

试试这样,

function ProcessData($arr){
    foreach($target as $key => $value){
        if( is_array($value) and !empty($value) ){
            ProcessData($value);
        }else{
            if($key == 'created'){
                echo $value;
            }
        }
    }
}