@nickname_null

Перебор json'а на php?

есть json:
{"part-1": {"t13": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8ceb0f8.jpg"], "t14": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cec0b4.jpg"], "t16": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cee086.jpg", "https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cee4c9.jpg"], "t19\u201320": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cf079c.jpg"], "t21\u201322": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cf2ae5.jpg", "https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cf2fa0.jpg"]}, "part-2": {"t5": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8d20a76.jpg"], "t6\u20137": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8d219bc.jpg"], "t8": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8d25274.jpg"]}}

нужно сделать цикл, который будет выводить:
$obj = json_decode(тут json, который сверху);
<div class="spoiler">
<div class="spoiler-head">* Часть</div>
<div class="spoiler-body extc">
<?php
foreach($obj as $k => $a) {
if ($k[0] !== 't') continue;
foreach($a as $l) {	
$res = '<a href="' . $k . '">' . str_replace('t','',$k) . '</a>';
echo $res;
echo '<br>';
}
}
?>
</div>
</div>

подскажите, как его сделать?
  • Вопрос задан
  • 383 просмотра
Решения вопроса 1
xEpozZ
@xEpozZ
Веб-разработчик
<?php
$json = '{"part-1": {"t13": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8ceb0f8.jpg"], "t14": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cec0b4.jpg"], "t16": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cee086.jpg", "https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cee4c9.jpg"], "t19\u201320": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cf079c.jpg"], "t21\u201322": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cf2ae5.jpg", "https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8cf2fa0.jpg"]}, "part-2": {"t5": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8d20a76.jpg"], "t6\u20137": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8d219bc.jpg"], "t8": ["https://gdz-ru.com/attachments/images/tasks/000/109/238/0002/5c373b8d25274.jpg"]}}';
$obj = json_decode($json, true);
?>
<div class="spoiler">
<div class="spoiler-head">* Часть</div>
<div class="spoiler-body extc">
<?php
foreach($obj as $k => $a) {
if ($k[0] !== 't') continue;
foreach($a as $l) {	
$res = '<a href="' . $k . '">' . str_replace('t','',$k) . '</a>';
echo $res;
echo '<br>';
}
}
?>
</div>
</div>
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы
30 нояб. 2024, в 22:41
500 руб./за проект
30 нояб. 2024, в 20:49
2000 руб./за проект
30 нояб. 2024, в 20:18
5000 руб./за проект