Не получается работать с json строкой как с массивом [дубликат]

133
15 октября 2019, 17:20

Данный вопрос является точным дубликатом:

  • json_decode выдает NULL, где ошибка? 2 ответа

Получаю вот такой ответ:

[{"id":233476,"title":"Pastel Travel Toothbrush Case","slug":"pastel-travel-toothbrush-case","clearcut":null,"black_score":null,"image":"https://thievemedia.imgix.net/2019/02/pastel-travel-toothbrush-case-featured-1.jpg","full_image_url":"https://s3.amazonaws.com/thieve-media/wp-content/uploads/2019/02/pastel-travel-toothbrush-case-featured-1.jpg","large_image_url":"https://s3.amazonaws.com/thieve-media/wp-content/uploads/2019/02/pastel-travel-toothbrush-case-featured-1.jpg","medium_image_url":"https://s3.amazonaws.com/thieve-media/wp-content/uploads/2019/02/pastel-travel-toothbrush-case-featured-1.jpg","original_price":"1.99","price":"1.79","discount":"10.05","rating":"4.8","total_orders":"574","affiliate_promotion_url":"http://s.click.aliexpress.com/e/cW1M4sxO","promotion_url":"https://api.thieve.co/view?id=MzI3NzMxNzU3NTE=","product_like_count":"87","product_user_likes":["2","30805","180229","229121","84864","226249","166954","176410","141218","196813","228510","142039","110622","225308","228028","228018","227760","224241","226566","225948"],"curator_id":"2","related_products":

[],"product_categories":[{"term_id":3641,"name":"Homeware","slug":"homeware","term_group":0,"term_taxonomy_id":3641,"taxonomy":"category","description":"","parent":0,"count":3077,"filter":"raw","cat_ID":3641,"category_count":3077,"category_description":"","cat_name":"Homeware","category_nicename":"homeware","category_parent":0}],"review_id":false,"review_slug":false,"review_excerpt":false,"review_video_id":false,"double_eleven_price":null,"double_eleven_discount":null,"sale_score":null,"lowest_price":"1.79"},{"id":233478,"title":"Preflight Checklist Tag","slug":"preflight-checklist-tag","clearcut":null,"black_score":null,"image":"https://thievemedia.imgix.net/2019/02/preflight-checklist-tag-featured-1.jpg","full_image_url":"https://s3.amazonaws.com/thieve-media/wp-content/uploads/2019/02/preflight-checklist-tag-featured-1.jpg","large_image_url":"https://s3.amazonaws.com/thieve-media/wp-content/uploads/2019/02/preflight-checklist-tag-featured-1.jpg","medium_image_url":"https://s3.amazonaws.com/thieve-media/wp-content/uploads/2019/02/preflight-checklist-tag-featured-1.jpg","original_price":"1.12","price":"0.90","discount":"19.64","rating":"4.9","total_orders":"3985","affiliate_promotion_url":"http://s.click.aliexpress.com/e/rQAnHQC","promotion_url":"https://api.thieve.co/view?id=MzI4Nzc5NzQwNDQ=","product_like_count":"58","product_user_likes":["2","225268","30805","229508","229121","226249","228993","176410","228807","196813","142039","221988","225882","207432","11351","226833","223904","63005","226677","188603"],"curator_id":"2","related_products":

[],"product_categories":[{"term_id":3641,"name":"Homeware","slug":"homeware","term_group":0,"term_taxonomy_id":3641,"taxonomy":"category","description":"","parent":0,"count":3077,"filter":"raw","cat_ID":3641,"category_count":3077,"category_description":"","cat_name":"Homeware","category_nicename":"homeware","category_parent":0}],"review_id":false,"review_slug":false,"review_excerpt":false,"review_video_id":false,"double_eleven_price":null,"double_eleven_discount":null,"sale_score":"0.47804495859273","lowest_price":"0.9"},{"id":233507,"title":"Silicone Zipper Pouch","slug":"silicone-zipper-pouch","clearcut":null,"black_score":null,"image":"https://thievemedia.imgix.net/2019/02/silicone-zipper-pouch-featured-1.jpg","full_image_url":"https://s3.amazonaws.com/thieve-media/wp-content/uploads/2019/02/silicone-zipper-pouch-featured-1.jpg","large_image_url":"https://s3.amazonaws.com/thieve-media/wp-content/uploads/2019/02/silicone-zipper-pouch-featured-1.jpg","medium_image_url":"https://s3.amazonaws.com/thieve-media/wp-content/uploads/2019/02/silicone-zipper-pouch-featured-1.jpg","original_price":"8.80","price":"7.48","discount":"15","rating":"5","total_orders":"17","affiliate_promotion_url":null,"promotion_url":"https://api.thieve.co/view?id=MzI5MTA3Mjg4OTQ=","product_like_count":"89","product_user_likes":["41347","2","225268","30805","229334","229508","227827","229203","229121","38150","84864","226249","226514","28881","227755","228748","68465","228510","226566","221988"],"curator_id":"2","related_products":[]

Хочу получить нужные мне значения из него:

$arr = json_decode($data);
foreach ($arr as $key => $value) {
    /* code */
}

Но в ответ получаю: Warning: Invalid argument supplied for foreach()

Что я упускаю? В чем моя ошибка?

Answer 1

$arr = json_decode($data, true); // конвертируем в ассоциативный массив

foreach ($arr as $key => $value) { /* code */ }

READ ALSO
Апач отображает пхп код вместо того чтобы его исполнять

Апач отображает пхп код вместо того чтобы его исполнять

Поставил в виртуалке CentOS 7, в него lamp (httpd и mariadb как обычно, а php 72 из remirepo)

103
Как добавить товары из корзины Woocommerce в форму?

Как добавить товары из корзины Woocommerce в форму?

На сайте есть форма, которая находится в корзине - ссылка на сайтКак сделать так, чтобы данные из формы захватывали каталог товаров из корзины...

129
Как отфильтровать массив через array walk и preg_match

Как отфильтровать массив через array walk и preg_match

Есть массив $bad, в нем содержатся запрещенные слова, есть массив $divs который не должен содержать элементы массива $bad, я пробую фильровать при...

147
MySQL. Сравнить два столбца из двух разных таблиц, но отобразить только отличающиеся

MySQL. Сравнить два столбца из двух разных таблиц, но отобразить только отличающиеся

Необходимо сравнить два столбца из двух разных таблиц, но отобразить только отличающиеся

147