Как мне пройти через объект json после преобразования в многомерный массив php? - PullRequest
0 голосов
/ 24 сентября 2018
{
"timeZone":"Africa/Lagos",
"days":
[
    {
        "Monday":["07:30 AM","08:00 AM","08:30 AM","09:00 AM","09:30 AM","10:00 AM","10:30 AM","11:00 AM","11:30 AM","12:00 PM","12:30 PM","01:00 PM","01:30 PM","02:00 PM","02:30 PM","03:00 PM","03:30 PM","04:00 PM","04:30 PM","05:00 PM","05:30 PM","06:00 PM","06:30 PM","07:00 PM"]
    },
    {
        "Tuesday":["07:30 AM","08:00 AM","08:30 AM","09:00 AM","09:30 AM","10:00 AM","10:30 AM","11:00 AM","11:30 AM","12:00 PM","12:30 PM","01:00 PM","01:30 PM","02:00 PM","02:30 PM","03:00 PM","03:30 PM","04:00 PM","04:30 PM","05:00 PM","05:30 PM","06:00 PM","06:30 PM","07:00 PM"]
    },
    {
        "Wednesday":["07:30 AM","08:00 AM","08:30 AM","09:00 AM","09:30 AM","10:00 AM","10:30 AM","11:00 AM","11:30 AM","12:00 PM","12:30 PM","01:00 PM","01:30 PM","02:00 PM","02:30 PM","03:00 PM","03:30 PM","04:00 PM","04:30 PM","05:00 PM","05:30 PM","06:00 PM","06:30 PM","07:00 PM"]
    },
    {
        "Thursday":["07:30 AM","08:00 AM","08:30 AM","09:00 AM","09:30 AM","10:00 AM","10:30 AM","11:00 AM","11:30 AM","12:00 PM","12:30 PM","01:00 PM","01:30 PM","02:00 PM","02:30 PM","03:00 PM","03:30 PM","04:00 PM","04:30 PM","05:00 PM","05:30 PM","06:00 PM","06:30 PM","07:00 PM"]
    },
    {
        "Friday":["07:30 AM","08:00 AM","08:30 AM","09:00 AM","09:30 AM","10:00 AM","10:30 AM","11:00 AM","11:30 AM","12:00 PM","12:30 PM","01:00 PM","01:30 PM","02:00 PM","02:30 PM","03:00 PM","03:30 PM","04:00 PM","04:30 PM","05:00 PM","05:30 PM","06:00 PM","06:30 PM","07:00 PM"]
    }
]
}

Как мне пройти через это, чтобы получить значения всех дней и их времени?$result['timeZone'] выводит значение правильно, а $result['days'] - нет.

 <?php 
   $result=json_decode($doctor->time_availiable,true);
   echo $result['timeZone'];

    foreach ($result['days'] as $key => $value) {
       echo $key.':'.$value;
    }

 ?>

Ответы [ 3 ]

0 голосов
/ 24 сентября 2018

Попробуйте ниже,

$results = json_decode($doctor->time_availiable, true);

foreach ($results as $key => $result)
{

    foreach ($results['days'] as $dayName => $timeAry)
    {
        //in timeAry you will get respective day times
    }
}
0 голосов
/ 24 сентября 2018

это приведет вас к часам, обратите внимание, некоторые из json декодируют как объект , а не как массив

$result=json_decode($doctor->time_availiable,true);

echo $ result-> timeZone;

foreach ($result['days'] as $key => $value) {
   foreach ($value as $weekday => $hours) {
        echo $weekday;
        foreach($hours as $hour) {
            echo $hour . ', ';
        }
   }    
}
0 голосов
/ 24 сентября 2018

В вашем примере $value внутри метода foreach() является массивом и не может быть отображен.

Попробуйте это:

<?php 
   $result=json_decode($doctor->time_availiable,true);
   echo $result['timeZone'];

    foreach ($result['days'] as $dayFromArray) {
        foreach($dayFromArray as $dayFromObject => $hours) {
            foreach($hours as $hour) {
                echo $day.':'.$hour."\n";   
            }
            echo "\n";
        }
    }
 ?>
...