Страницы

Поиск по вопросам

пятница, 15 марта 2019 г.

json_decode($string, true)

Дано:
$str = '{"1013":[{"type":6,"value":"The hormone hCG is released when you conceive, and may alter your mood."},{"type":8,"value":"Make sure you log your data to find a pattern between your symptoms, and whether or not you conceived."},{"type":7,"value":"You are approaching the end of your menstrual cycle and of the luteal phase. Progesterone will continue to be produced until the corpus luteum breaks down about 14 days after ovulation (it does not break down if an embryo implants). At this point, your uterine lining is of no use so your period will begin."}]}'; echo $str; echo "

";
Используя $json = json_decode($string, true) нужно вывести на экран данные касательно только {"type":8, "value"}. Как это правильно сделать? Пожалуйста подскажите! Особых идей нет, kак вариант:
$str = '{"1013":[{"type":6,"value":"The hormone hCG is released when you conceive, and may alter your mood."},{"type":8,"value":"Make sure you log your data to find a pattern between your symptoms, and whether or not you conceived."},{"type":7,"value":"You are approaching the end of your menstrual cycle and of the luteal phase. Progesterone will continue to be produced until the corpus luteum breaks down about 14 days after ovulation (it does not break down if an embryo implants). At this point, your uterine lining is of no use so your period will begin."}]}'; echo $str; echo "

";
$element1 = array(); $element1["type"] = 6; $element1["value"] = "The hormone hCG is released when you conceive, and may alter your mood.";
$element2 = array(); $element2["type"] = 8; $element2["value"] = "Make sure you log your data to find a pattern between your symptoms, and whether or not you conceived.";
$element3 = array(); $element3["type"] = 7; $element3["value"] = "You are approaching the end of your menstrual cycle and of the luteal phase. Progesterone will continue to be produced until the corpus luteum breaks down about 14 days after ovulation (it does not break down if an embryo implants). At this point, your uterine lining is of no use so your period will begin.";
$elements = array(); $elements[0] = $element1; $elements[1] = $element2; $elements[2] = $element3;
$final = array(); $final[1013] = $elements;
$json = json_decode($final, true); echo $json;


Ответ

$json = json_decode($string, true); // раскодировать в виде ассоциативного массива foreach($json['1013'] as $elem) { echo $elem['type'], " ", $elem['value']; }
Просто пройтись по массиву в поле '1013'
{ "1013": [{"type":6,"value":"The hormone hCG is released when you conceive, and may alter your mood."}, {"type":8,"value":"Make sure you log your data to find a pattern between your symptoms, and whether or not you conceived."}, {"type":7,"value":"You are approaching the end of your menstrual cycle and of the luteal phase. Progesterone will continue to be produced until the corpus luteum breaks down about 14 days after ovulation (it does not break down if an embryo implants). At this point, your uterine lining is of no use so your period will begin."}] }
Запись предстваляет собой объект с 1 полем '1013', в этом поле лежит массив из 3 объектов, каждый из которых состоит из поля 'type' и 'value'
json_decode($string, true); -раскодирует json, второй аргумент означает, что не в виде объекта, а в виде ассоциативного массива. Вот и получается, что мы проходим по массиву, хранящемуся в 1м поле

Комментариев нет:

Отправить комментарий