I
I
Ilya Trusov2015-10-28 00:01:52
PHP
Ilya Trusov, 2015-10-28 00:01:52

How to select the necessary elements from json?

Hello. Help solve the issue.
This json is returned:
biBTyvf.png
How to select all elements (in php) lying in data->images->low_resolution->url.
Thanks for the help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
maximw, 2015-10-28
@artgrosvil

$array = json_decode($json, true);
$result = array();
foreach($array['data'] as $item) {
  $result[] = $item['images']['low_resolution']['url'];
}

If $item['images'] is also an array, then one more nested loop is needed.

A
Aleksey Ratnikov, 2015-10-28
@mahoho

Use array_column()

$urls = array_column(array_column(array_column(array_column(json_decode($jsonString ,true), 'data'), 'images'), 'low_resolution'), 'url');

Not very pretty, but fast and native. You can also try using the JsonPath library

R
Rsa97, 2015-10-28
@Rsa97

json_decode()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question