Я пытаюсь преобразовать этот ответ в PHP массив:Как преобразовать ответ JSON в PHP массив
string(734) " {"definitions":[{"text":"Informal One who is proficient at using or programming a computer; a computer buff.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"Informal One who uses programming skills to gain illegal access to a computer network or file.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"Informal One who enthusiastically pursues a game or sport: a weekend tennis hacker. ","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"See hackie.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"}]}"
Я попробовал все, от
$string = $result; $pattern = '\w*\(\d+\)\s'; $replacement = ''; preg_replace($pattern,$replacement,$string)
к
$string = $result; $pattern = '\w*\(\d+\)\s'; $replacement = ''; $def_array = str_replace($pattern,$replacement,$string);
, но когда я делаю это, они даже не меняют исходный результат.
Я хочу, чтобы достичь чего-то вроде этого: $def_array = {"definitions":[{"text":"Informal One who is proficient at using or programming a computer; a computer buff.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"Informal One who uses programming skills to gain illegal access to a computer network or file.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"Informal One who enthusiastically pursues a game or sport: a weekend tennis hacker. ","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"},{"text":"See hackie.","attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"}]}
Я хочу, чтобы пройти через каждое определение, как это:
for($i = 0; $i < sizeof($def_array[0]["definitions"]); $i++) { echo $def_array[0]["definitions"][$i]["text"]; echo "\n"; }
Я не использую json_decode, потому что, когда я могу получить ошибка Fatal error: Cannot use object of type stdClass as array on the line
for ($ i = 0; $ i < sizeof ($ def_array [0] ["определения"]); $ i ++) `.
Не забудьте выбрать ответ, если вы зафиксировали проблему. – NMC