The question is trivial, but I can't understand how I can parse the array into keys and values, then to put them in 1 variable and send it by mail() function

  object(stdClass)#5 (2) { ["Супермясная 3"]=> object(stdClass)#4 (6) { ["price"]=> string(3) "695" ["quantity"]=> string(1) "1" ["description"]=> string(0) "" ["totalPrice"]=> int(695) ["name"]=> string(24) "Супермясная 3" ["productSize"]=> string(7) "27 см" } ["Сытные палочки с чоризо"]=> object(stdClass)#6 (6) { ["price"]=> int(295) ["quantity"]=> string(1) "1" ["description"]=> string(222) "Легкие брусочки тонкого теста, покрытые сыром и начинкой из пикантных колбасок чоризо с соусом Чипотл Саусвест. 8 палочек" ["totalPrice"]=> int(295) ["name"]=> string(43) "Сытные палочки с чоризо" ["image"]=> string(31) "assets/img/zakuski/palochka.jpg" } } 

This array turned out after I parsed JSON , its length can be any, how can I iterate it and select the necessary fields to send to mail() ? Thank you in advance!

PS initial JSON

 {"Супермясная 3":{"price":"695","quantity":"1","description":"","totalPrice":695,"name":"Супермясная 3","productSize":"27 см"},"Сытные палочки с чоризо":{"price":295,"quantity":"1","description":"Легкие брусочки тонкого теста, покрытые сыром и начинкой из пикантных колбасок чоризо с соусом Чипотл Саусвест. 8 палочек","totalPrice":295,"name":"Сытные палочки с чоризо","image":"assets/img/zakuski/palochka.jpg"}} 
  • one
    array, this is the key and the value, set the second parameter json_decode to true, and get the array and not the objects. - teran 2:26 pm

2 answers 2

In my opinion everything is simple:

 $json = '{"Супермясная 3":{"price":"695","quantity":"1","description":"","totalPrice":695,"name":"Супермясная 3","productSize":"27 см"},"Сытные палочки с чоризо":{"price":295,"quantity":"1","description":"Легкие брусочки тонкого теста, покрытые сыром и начинкой из пикантных колбасок чоризо с соусом Чипотл Саусвест. 8 палочек","totalPrice":295,"name":"Сытные палочки с чоризо","image":"assets/img/zakuski/palochka.jpg"}}'; $data = json_decode($json, true); foreach ($data as $key => $values) { // здесь отбираешь что надо } 

    This is not an array, but an object. To get an array, you need json_decode($jsonString,true); And then the easiest option is brute force or any helper for arrays