Требуется извлечь из всех узлов JSON
-строки значения с идентификатором "text"
с целью собрать на выходе выражение (предложение).
Вопрос: как перебрать все элементы данной JSON
-строки?
{
"language": "ru",
"textAngle": 0.0,
"orientation": "Up",
"regions": [
{
"boundingBox": "42,12,596,465",
"lines": [
{
"boundingBox": "42,12,557,49",
"words": [
{
"boundingBox": "42,13,64,41",
"text": "ты"
},
{
"boundingBox": "120,13,175,48",
"text": "БУДЕШЬ"
},
{
"boundingBox": "309,12,198,43",
"text": "визжать"
},
{
"boundingBox": "521,13,78,41",
"text": "как"
}
]
},
{
"boundingBox": "180,71,286,49",
"words": [
{
"boundingBox": "180,71,286,49",
"text": "МАНДРАГОРА,"
}
]
},
{
"boundingBox": "70,422,506,49",
"words": [
{
"boundingBox": "70,422,135,49",
"text": "когда"
},
{
"boundingBox": "216,422,235,43",
"text": "ПОЛУЧИШЬ"
},
{
"boundingBox": "465,423,111,41",
"text": "мня"
}
]
},
{
"boundingBox": "569,465,69,12",
"words": [
{
"boundingBox": "569,465,54,12",
"text": "riSOVach."
},
{
"boundingBox": "625,469,13,7",
"text": "ru"
}
]
}
]
}
]
}
$json_string = '{
"language":"ru",
"textAngle":0.0,
"orientation":"Up",
"regions":[
{
"boundingBox":"42,12,596,465",
"lines":[
{
"boundingBox":"42,12,557,49",
"words":[
{
"boundingBox":"42,13,64,41",
"text":"ты"
},
{
"boundingBox":"120,13,175,48",
"text":"БУДЕШЬ"
},
{
"boundingBox":"309,12,198,43",
"text":"визжать"
},
{
"boundingBox":"521,13,78,41",
"text":"как"
}
]
},
{
"boundingBox":"180,71,286,49",
"words":[
{
"boundingBox":"180,71,286,49",
"text":"МАНДРАГОРА,"
}
]
},
{
"boundingBox":"70,422,506,49",
"words":[
{
"boundingBox":"70,422,135,49",
"text":"когда"
},
{
"boundingBox":"216,422,235,43",
"text":"ПОЛУЧИШЬ"
},
{
"boundingBox":"465,423,111,41",
"text":"мня"
}
]
},
{
"boundingBox":"569,465,69,12",
"words":[
{
"boundingBox":"569,465,54,12",
"text":"riSOVach."
},
{
"boundingBox":"625,469,13,7",
"text":"ru"
}
]
}
]
}
]
}';
$obj=json_decode($json_string);
Решение №1 - не рекомендуемое, для разминки с циклом for
for ($n = 0; $n < count($obj->regions); $n++) {
for ($j = 0; $j < count($obj->regions[$n]->lines); $j++) {
for ($i = 0; $i < count($obj->regions[$n]->lines[$j]->words); $i++) {
echo $obj->regions[$n]->lines[$j]->words[$i]->text." ";
}
}
}
Решение №2 - рекомендуемое
foreach($obj->regions as $regions) {
foreach($regions->lines as $lines) {
foreach($lines->words as $words) {
echo $words->text . " ";
}
}
}
Есть такой код, который не работаетКласс работает правильно, проблема только в функции header();
На хостинге этот код работает как часы, на домашнем в браузере ошибка 500 в приложении которое отправляет запрос ошибка 501
Например, плагины социалок, галерей и так далееКогда то знал 1 сайт, не могу его найти