PHP Echo嵌套数组值

时间:2017-07-23 14:22:25

标签: php arrays

我想回复CATEGORIA在Booking_meta下但嵌套值我无法回应,这是print_r结果"

我可以访问1级数组,如:

echo $EM_Booking-> booking_id;

但嵌套在[registration]和[booking]数组中我没有能够回显结果,在使用数组时我有点弱:)

EM_Booking Object
(
[booking_id] => 13
[event_id] => 31
[person_id] => 0
[booking_price] => 0.0000
[booking_spaces] => 1
[booking_comment] => 
[booking_status] => 1
[booking_tax_rate] => 0
[booking_taxes] => 
[booking_meta] => Array
    (
        [registration] => Array
            (
                [user_name] => rb
                [first_name] => rb
                [last_name] => 
                [dbem_email] => walterl81@hotmail.com
                [user_email] => walterl81@hotmail.com
                [dbem_phone] => 33446667678
                [dbem_societa] => ttf
                [dbem_ente] => FCI (Federazione Ciclistica Italiana)
                [dbem_cod_societa] => 6666
                [dbem_tessera_n_] => 3344
            )

        [booking] => Array
            (
                [categoria] => M2 35-39 (nati 1978/1982)
                [dbem_tesseramento] => 
            )

    ) 

2 个答案:

答案 0 :(得分:1)

Arrays内的Object内容是// Here is how to access array // This is your reg array $reg_array = $EM_Booking->booking_meta['registration']; // This is your booking array $booking_array = $EM_Booking->booking_meta['booking']; // Uncomment below if you wanna see array // print_r($reg_array); // print_r($booking_array); // either through array echo $booking_array['categoria']; // Or directly echo $EM_Booking->booking_meta['booking']['categoria'] ,因此您可以轻松完成的工作如下

$myobject = json_decode( json_encode($EM_Booking) );

// and then
echo $myobject->booking_meta->registration->user_name;

echo $myobject->booking_meta->booking->categoria;

或者

open System

type Point(X:int,Y:int) =
  member this.x = X
  member this.y = Y

let fib  = 
    seq {
        for i in 1..10 do 
            yield Point(1,2) 
        }

fib |> printf("%A")

Console.ReadLine() |> ignore

答案 1 :(得分:1)

要访问[注册]和[预订]中的数据,您需要编写类似的行:

echo $EM_Booking->booking_meta['registration']['user_name'];
echo $EM_Booking->booking_meta['booking']['categoria'];

您不能使用以下内容:

$EM_Booking->booking_meta->registration->user_name

因为[booking_meta]和[booking]中存储的数据不是对象。如果查看转储数据,它们就是数组,因此访问它们的方式不同。