Ir para conteúdo
Fórum Script Brasil
  • 0

como faço um foreach no json


adrsublimacao

Pergunta

dentro do meu json tenho os itens do pedido como faço um foreach pra poder salvar esses valores em variaveis pra poder salvar no banco os itens do pedido quando em um um item so é facil porque não precisa de loop ai quando tenho mais de um item ai não sei como fazer pra pegar os itens do pedido e salvar em variaveis separada pra salvar no banco
 
 "items": [
        {
            "index"1,
            "id""26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId""3fc4a0e3-f25e-46f7-a1e4-b66ecdb88152",
            "name""PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)",
            "externalCode""t02",
            "unit""UN",
            "quantity"1,
            "unitPrice"0.00,
            "optionsPrice"24.00,
            "totalPrice"24.00,
            "options": [
                {
                    "index"2,
                    "id""a8b24880-b3f6-390a-8514-77bd7b2055aa",
                    "name""Calabresa",
                    "externalCode""c02-i002",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"20.00,
                    "addition"0.00,
                    "price"20.00
                },
                {
                    "index"3,
                    "id""893622be-1730-318e-a486-b2dd4fdfa87d",
                    "name""Massa Fina + Borda Recheada",
                    "externalCode""m02",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"4.00,
                    "addition"0.00,
                    "price"4.00
                }
            ],
            "price"0.00
        },
        {
            "index"4,
            "id""b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId""663ff728-af46-41ae-bb8d-4ffcc1b961e4",
            "name""PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"10.00,
            "optionsPrice"10.00,
            "totalPrice"20.00,
            "options": [
                {
                    "index"5,
                    "id""d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name""Laranja",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"10.00,
                    "addition"0.00,
                    "price"10.00
                }
            ],
            "price"10.00
        },
        {
            "index"6,
            "id""e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId""98de1e93-ba34-48bb-8f75-1b8c21e43fd1",
            "name""PEDIDO DE TESTE - Sanduíche",
            "externalCode""c01-i001",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"20.00,
            "optionsPrice"1.00,
            "totalPrice"21.00,
            "options": [
                {
                    "index"7,
                    "id""1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name""Complemento 1",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"1.00,
                    "addition"0.00,
                    "price"1.00
                },
                {
                    "index"8,
                    "id""42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name""Complemento 6",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"0.00,
                    "addition"0.00,
                    "price"0.00
                }
            ],
            "price"20.00
        }
    ],
Link para o comentário
Compartilhar em outros sites

8 respostass a esta questão

Posts Recomendados

  • 2

segue sugestão:
 

<?php
$jsonData = json_decode('{"items": [
        {
            "index": 1,
            "id": "26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId": "3fc4a0e3-f25e-46f7-a1e4-b66ecdb88152",
            "name": "PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)",
            "externalCode": "t02",
            "unit": "UN",
            "quantity": 1,
            "unitPrice": 0.00,
            "optionsPrice": 24.00,
            "totalPrice": 24.00,
            "options": [
                {
                    "index": 2,
                    "id": "a8b24880-b3f6-390a-8514-77bd7b2055aa",
                    "name": "Calabresa",
                    "externalCode": "c02-i002",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 20.00,
                    "addition": 0.00,
                    "price": 20.00
                },
                {
                    "index": 3,
                    "id": "893622be-1730-318e-a486-b2dd4fdfa87d",
                    "name": "Massa Fina + Borda Recheada",
                    "externalCode": "m02",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 4.00,
                    "addition": 0.00,
                    "price": 4.00
                }
            ],
            "price": 0.00
        },
        {
            "index": 4,
            "id": "b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId": "663ff728-af46-41ae-bb8d-4ffcc1b961e4",
            "name": "PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit": "GRAMS",
            "quantity": 1,
            "unitPrice": 10.00,
            "optionsPrice": 10.00,
            "totalPrice": 20.00,
            "options": [
                {
                    "index": 5,
                    "id": "d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name": "Laranja",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 10.00,
                    "addition": 0.00,
                    "price": 10.00
                }
            ],
            "price": 10.00
        },
        {
            "index": 6,
            "id": "e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId": "98de1e93-ba34-48bb-8f75-1b8c21e43fd1",
            "name": "PEDIDO DE TESTE - Sanduíche",
            "externalCode": "c01-i001",
            "unit": "GRAMS",
            "quantity": 1,
            "unitPrice": 20.00,
            "optionsPrice": 1.00,
            "totalPrice": 21.00,
            "options": [
                {
                    "index": 7,
                    "id": "1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name": "Complemento 1",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 1.00,
                    "addition": 0.00,
                    "price": 1.00
                },
                {
                    "index": 8,
                    "id": "42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name": "Complemento 6",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 0.00,
                    "addition": 0.00,
                    "price": 0.00
                }
            ],
            "price": 20.00
        }
    ]}');

foreach ($jsonData->items as $v) {
  echo '<pre>$v: ';
  print_r($v);
  echo '</pre>';
}

 

Editado por wash
Link para o comentário
Compartilhar em outros sites

  • 1

Isso daí é bem difícil, faz dois anos que eu tento resolver esse tipo de problema, e nunca consegui.
Você diz que isso daí é json, mas aqui no meu PC eu vejo apenas como string.
Eu tentei transformar o string em json, assim $json=json_decode($string), mas a resposta que eu tive foi null.
Durante dois anos eu desisti de resolver esse tipo de problema, mas hoje eu tive a coragem de pegar o editor de texto, e transformar tudo que é { em [, e tudo que é } em ], e tudo que é : em =>, ou seja, montei um array. Aí sim eu consegui fazer alguma coisa! Olha só:
 

<?php
 $adr=["items"=> [
        [   "index"=> 1,
            "id"=> "26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId"=> "3fc4a0e3-f25e-46f7-a1e4-b66ecdb88152",
            "name"=> "PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)","externalCode"=> "t02",
            "unit"=> "UN","quantity"=> 1,"unitPrice"=> 0.00,"optionsPrice"=> 24.00,
            "totalPrice"=> 24.00,
            "options"=> [
                [   "index"=> 2,"id"=> "a8b24880-b3f6-390a-8514-77bd7b2055aa",
                    "name"=> "Calabresa","externalCode"=> "c02-i002",
                    "unit"=> "UN","quantity"=> 1,"unitPrice"=> 20.00,
                    "addition"=> 0.00,"price"=> 20.00],
                [   "index"=> 3,"id"=> "893622be-1730-318e-a486-b2dd4fdfa87d",
                    "name"=> "Massa Fina + Borda Recheada","externalCode"=> "m02",
                    "unit"=> "UN","quantity"=> 1,"unitPrice"=> 4.00,
                    "addition"=> 0.00,"price"=> 4.00]],
            "price"=> 0.00],
        [   "index"=> 4,
            "id"=> "b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId"=> "663ff728-af46-41ae-bb8d-4ffcc1b961e4",
            "name"=> "PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit"=> "GRAMS","quantity"=> 1,"unitPrice"=> 10.00,"optionsPrice"=> 10.00,
            "totalPrice"=> 20.00,
            "options"=> [
                [   "index"=> 5,"id"=> "d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name"=> "Laranja",
                    "unit"=> "UN","quantity"=> 1,"unitPrice"=> 10.00,
                    "addition"=> 0.00,"price"=> 10.00]],
            "price"=> 10.00],
        [   "index"=> 6,
            "id"=> "e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId"=> "98de1e93-ba34-48bb-8f75-1b8c21e43fd1",
            "name"=> "PEDIDO DE TESTE - Sanduíche",
            "externalCode"=> "c01-i001",
            "unit"=> "GRAMS","quantity"=> 1,"unitPrice"=> 20.00,"optionsPrice"=> 1.00,
            "totalPrice"=> 21.00,
            "options"=> [
                [   "index"=> 7,
                    "id"=> "1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name"=> "Complemento 1",
                    "unit"=> "UN","quantity"=> 1,"unitPrice"=> 1.00,
                    "addition"=> 0.00,"price"=> 1.00],
                [   "index"=> 8,
                    "id"=> "42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name"=> "Complemento 6",
                    "unit"=> "UN","quantity"=> 1,"unitPrice"=> 0.00,
                    "addition"=> 0.00,"price"=> 0.00]],
            "price"=> 20.00]]];
//var_dump($adr);
foreach($adr as $a){
    foreach($a as $b){
        echo $b['name']."<br>";
    }
}
/* resultado: 
PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)
PEDIDO DE TESTE - Bebida teste 100 ml
PEDIDO DE TESTE - Sanduíche
*/


mas, no seu caso, se você tiver um json de verdade, acho que esse código vai funcionar

foreach($meujson as $items) {
	foreach($items as $item) {
		echo $item->name; }}

A minha sugestão é sempre trabalhar com a função var_dump(), esse é o melhor caminho para estudar o PHP, e estudar a função dd() no caso do Laravel.

Editado por Frank K Hosaka
Link para o comentário
Compartilhar em outros sites

  • 1

Bom, eu não sou especialista como informei logo no começo da minha resposta. Tudo que eu faço é tentativa e erro, já esse último response que você passou foi bem fácil de trabalhar.

Eu não tenho como pegar o seu json, mas apenas o string que aparece na tela. Peguei o string e...

 

<?php
$string= (aqui eu copiei o response que foi divulgado na mensagem)
$json=json_decode($string);
echo "Pedido ".$json->id."<br>";
echo "Cliente ".$json->customer->name."<br>";
echo "itens ".count($json->items)."<p>";
$items=$json->items;
foreach($items as $item){
    echo $item->name."<br>";
    echo "adicionais ".count($item->options)."<br>";
    foreach($item->options as $option){
        echo $option->name."<br>";
    } echo "<p>";
    }
      
Resultado:
Pedido 5e58177c-d8f2-41cf-9277-7fa742806f03
Cliente PEDIDO DE TESTE - adriano rosa do prado
itens 3
PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)
adicionais 2
Portuguesa
Massa Tradicional + Borda Recheada

PEDIDO DE TESTE - Bebida teste 100 ml
adicionais 1
Laranja

PEDIDO DE TESTE - Sanduíche
adicionais 2
Complemento 1
Complemento 6

Ou seja, eu só faço tentativa e erro. Como o seu response é um json, você não precisa da primeira linha desse código, apenas eu.

Editado por Frank K Hosaka
Link para o comentário
Compartilhar em outros sites

  • 1
4 horas atrás, wash disse:

segue sugestão:
 

<?php
$jsonData = json_decode('{"items": [
        {
            "index": 1,
            "id": "26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId": "3fc4a0e3-f25e-46f7-a1e4-b66ecdb88152",
            "name": "PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)",
            "externalCode": "t02",
            "unit": "UN",
            "quantity": 1,
            "unitPrice": 0.00,
            "optionsPrice": 24.00,
            "totalPrice": 24.00,
            "options": [
                {
                    "index": 2,
                    "id": "a8b24880-b3f6-390a-8514-77bd7b2055aa",
                    "name": "Calabresa",
                    "externalCode": "c02-i002",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 20.00,
                    "addition": 0.00,
                    "price": 20.00
                },
                {
                    "index": 3,
                    "id": "893622be-1730-318e-a486-b2dd4fdfa87d",
                    "name": "Massa Fina + Borda Recheada",
                    "externalCode": "m02",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 4.00,
                    "addition": 0.00,
                    "price": 4.00
                }
            ],
            "price": 0.00
        },
        {
            "index": 4,
            "id": "b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId": "663ff728-af46-41ae-bb8d-4ffcc1b961e4",
            "name": "PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit": "GRAMS",
            "quantity": 1,
            "unitPrice": 10.00,
            "optionsPrice": 10.00,
            "totalPrice": 20.00,
            "options": [
                {
                    "index": 5,
                    "id": "d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name": "Laranja",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 10.00,
                    "addition": 0.00,
                    "price": 10.00
                }
            ],
            "price": 10.00
        },
        {
            "index": 6,
            "id": "e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId": "98de1e93-ba34-48bb-8f75-1b8c21e43fd1",
            "name": "PEDIDO DE TESTE - Sanduíche",
            "externalCode": "c01-i001",
            "unit": "GRAMS",
            "quantity": 1,
            "unitPrice": 20.00,
            "optionsPrice": 1.00,
            "totalPrice": 21.00,
            "options": [
                {
                    "index": 7,
                    "id": "1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name": "Complemento 1",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 1.00,
                    "addition": 0.00,
                    "price": 1.00
                },
                {
                    "index": 8,
                    "id": "42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name": "Complemento 6",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 0.00,
                    "addition": 0.00,
                    "price": 0.00
                }
            ],
            "price": 20.00
        }
    ]}');

foreach ($jsonData->items as $v) {
  echo '<pre>$v: ';
  print_r($v);
  echo '</pre>';
}

 

Bacana, Wash! Faz dois anos que eu quebro a cabeça com esse json_decode, mas nunca imaginei em colocar o string diretamente nele, sempre coloquei num outro string, e o Json_decode me retornava null. Valeu, viver é aprender!

Link para o comentário
Compartilhar em outros sites

  • 0
14 horas atrás, adrsublimacao disse:
dentro do meu json tenho os itens do pedido como faço um foreach pra poder salvar esses valores em variaveis pra poder salvar no banco os itens do pedido quando em um um item so é facil porque não precisa de loop ai quando tenho mais de um item ai não sei como fazer pra pegar os itens do pedido e salvar em variaveis separada pra salvar no banco
 
 "items": [
        {
            "index"1,
            "id""26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId""3fc4a0e3-f25e-46f7-a1e4-b66ecdb88152",
            "name""PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)",
            "externalCode""t02",
            "unit""UN",
            "quantity"1,
            "unitPrice"0.00,
            "optionsPrice"24.00,
            "totalPrice"24.00,
            "options": [
                {
                    "index"2,
                    "id""a8b24880-b3f6-390a-8514-77bd7b2055aa",
                    "name""Calabresa",
                    "externalCode""c02-i002",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"20.00,
                    "addition"0.00,
                    "price"20.00
                },
                {
                    "index"3,
                    "id""893622be-1730-318e-a486-b2dd4fdfa87d",
                    "name""Massa Fina + Borda Recheada",
                    "externalCode""m02",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"4.00,
                    "addition"0.00,
                    "price"4.00
                }
            ],
            "price"0.00
        },
        {
            "index"4,
            "id""b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId""663ff728-af46-41ae-bb8d-4ffcc1b961e4",
            "name""PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"10.00,
            "optionsPrice"10.00,
            "totalPrice"20.00,
            "options": [
                {
                    "index"5,
                    "id""d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name""Laranja",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"10.00,
                    "addition"0.00,
                    "price"10.00
                }
            ],
            "price"10.00
        },
        {
            "index"6,
            "id""e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId""98de1e93-ba34-48bb-8f75-1b8c21e43fd1",
            "name""PEDIDO DE TESTE - Sanduíche",
            "externalCode""c01-i001",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"20.00,
            "optionsPrice"1.00,
            "totalPrice"21.00,
            "options": [
                {
                    "index"7,
                    "id""1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name""Complemento 1",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"1.00,
                    "addition"0.00,
                    "price"1.00
                },
                {
                    "index"8,
                    "id""42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name""Complemento 6",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"0.00,
                    "addition"0.00,
                    "price"0.00
                }
            ],
            "price"20.00
        }
    ],

pra ficar mais claro esse é o $response que recebo 

{
    "id""5e58177c-d8f2-41cf-9277-7fa742806f03",
    "delivery": {
        "mode""DEFAULT",
        "deliveredBy""MERCHANT",
        "deliveryDateTime""2022-12-30T19:05:40.933Z",
        "observations""Portão da casa/prédio",
        "deliveryAddress": {
            "streetName""PEDIDO DE TESTE - NÃO ENTREGAR - Ramal Bujari",
            "streetNumber""100",
            "formattedAddress""PEDIDO DE TESTE - NÃO ENTREGAR - Ramal Bujari, 100",
            "neighborhood""Bujari",
            "postalCode""00000000",
            "city""Bujari",
            "state""AC",
            "country""BR",
            "coordinates": {
                "latitude"-9.822081,
                "longitude"-67.948635
            }
        }
    },
    "orderType""DELIVERY",
    "orderTiming""IMMEDIATE",
    "displayId""0179",
    "createdAt""2022-12-30T18:15:40.933Z",
    "preparationStartDateTime""2022-12-30T18:15:40.933Z",
    "isTest"true,
    "merchant": {
        "id""b053eb1d-294a-4a42-806b-a46f5e6517b6",
        "name""Teste - PED NO WHATS"
    },
    "customer": {
        "id""932ab1fc-f076-4a41-b6d8-838ecf646cad",
        "name""PEDIDO DE TESTE - adriano rosa do prado",
        "phone": {
            "number""0800 007 0110",
            "localizer""28528522",
            "localizerExpiration""2022-12-30T21:15:40.933Z"
        },
        "ordersCountOnMerchant"0
    },
    "items": [
        {
            "index"1,
            "id""26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId""1da2022d-8e2c-4025-b095-34935203f239",
            "name""PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)",
            "externalCode""t02",
            "unit""UN",
            "quantity"1,
            "unitPrice"0.00,
            "optionsPrice"25.00,
            "totalPrice"25.00,
            "options": [
                {
                    "index"2,
                    "id""a478fba7-ebe9-38d1-8b71-8897f02d2e31",
                    "name""Portuguesa",
                    "externalCode""c02-i001",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"22.00,
                    "addition"0.00,
                    "price"22.00
                },
                {
                    "index"3,
                    "id""cc8831f7-f486-3d37-be6c-8d85c90920da",
                    "name""Massa Tradicional + Borda Recheada",
                    "externalCode""m01",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"3.00,
                    "addition"0.00,
                    "price"3.00
                }
            ],
            "price"0.00
        },
        {
            "index"4,
            "id""b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId""b369521e-0b21-449a-a754-3f5fbf3549a5",
            "name""PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"10.00,
            "optionsPrice"10.00,
            "totalPrice"20.00,
            "options": [
                {
                    "index"5,
                    "id""d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name""Laranja",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"10.00,
                    "addition"0.00,
                    "price"10.00
                }
            ],
            "price"10.00
        },
        {
            "index"6,
            "id""e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId""1361931c-ccc0-4889-b495-2f8319d08ec7",
            "name""PEDIDO DE TESTE - Sanduíche",
            "externalCode""c01-i001",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"20.00,
            "optionsPrice"1.00,
            "totalPrice"21.00,
            "options": [
                {
                    "index"7,
                    "id""1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name""Complemento 1",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"1.00,
                    "addition"0.00,
                    "price"1.00
                },
                {
                    "index"8,
                    "id""42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name""Complemento 6",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"0.00,
                    "addition"0.00,
                    "price"0.00
                }
            ],
            "price"20.00
        }
    ],
    "salesChannel""IFOOD",
    "total": {
        "subTotal"66.00,
        "deliveryFee"8.90,
        "benefits"0,
        "orderAmount"74.90,
        "additionalFees"0.00
    },
    "payments": {
        "prepaid"74.90,
        "pending"0,
        "methods": [
            {
                "value"74.90,
                "currency""BRL",
                "method""CREDIT",
                "type""ONLINE",
                "card": {
                    "brand""VISA"
                },
                "prepaid"true
            }
        ]
    }
}
 
nesse exemplo tenho 3 produtos em ITEM que são os index 1, 4 e 6 
e os adicionais dos produtos que são os index 2,3,5,7,8
 
que saber como pegar em um foreach e fazer tambem um for quando tiver adicionais ou não 
 
quando tem um produto apenas eu faco assim
 
$jresponse=json_decode($response);
  
  $id=$jresponse->id;
  $delivery_mode=$jresponse->delivery->mode;
  $delivery_deliveredBy=$jresponse->delivery->deliveredBy;
  $delivery_deliveryDateTime=$jresponse->delivery->deliveryDateTime;
  $delivery_observations=$jresponse->delivery->observations;
  
  $delivery_deliveryAddress_streetName=$jresponse->delivery->deliveryAddress->streetName;
  $delivery_deliveryAddress_streetNumber=$jresponse->delivery->deliveryAddress->streetNumber;
  $delivery_deliveryAddress_formattedAddress=$jresponse->delivery->deliveryAddress->formattedAddress;
  $delivery_deliveryAddress_neighborhood=$jresponse->delivery->deliveryAddress->neighborhood;
  $delivery_deliveryAddress_postalCode=$jresponse->delivery->deliveryAddress->postalCode;        
  $delivery_deliveryAddress_city=$jresponse->delivery->deliveryAddress->city;    
  $delivery_deliveryAddress_state=$jresponse->delivery->deliveryAddress->state;    
  $delivery_deliveryAddress_country=$jresponse->delivery->deliveryAddress->country;    
  
  $delivery_deliveryAddress_coordinates_latitude=$jresponse->delivery->deliveryAddress->coordinates->latitude;    
  $delivery_deliveryAddress_coordinates_longitude=$jresponse->delivery->deliveryAddress->coordinates->longitude;
    
  $orderType=$jresponse->orderType;
  $orderTiming=$jresponse->orderTiming;
  $displayId=$jresponse->displayId;
  $createdAt=$jresponse->createdAt;
  $preparationStartDateTime=$jresponse->preparationStartDateTime;
  $isTest=$jresponse->isTest;
  
  $merchant_id=$jresponse->merchant->id;
  $merchant_name=$jresponse->merchant->name;
  
  $customer_id=$jresponse->customer->id;
  $customer_name=$jresponse->customer->name;
  
  $customer_phone_number=$jresponse->customer->phone->number;
  $customer_phone_localizer=$jresponse->customer->phone->localizer;
  $customer_phone_localizerExpiration=$jresponse->customer->phone->localizerExpiration;
  
  $customer_ordersCountOnMerchant=$jresponse->customer->ordersCountOnMerchant;
    
  $items_index=$jresponse->items[0]->index;
  $items_id=$jresponse->items[0]->id;
  $items_uniqueId=$jresponse->items[0]->uniqueId;
  $items_name=$jresponse->items[0]->name;
    
  $items_index2=$jresponse->items[1]->index;
  $items_id2=$jresponse->items[1]->id;
  $items_uniqueId2=$jresponse->items[1]->uniqueId;
  $items_name2=$jresponse->items[1]->name;
  
  $items_index3=$jresponse->items[2]->index;
  $items_id3=$jresponse->items[2]->id;
  $items_uniqueId3=$jresponse->items[2]->uniqueId;
  $items_name3=$jresponse->items[2]->name;
  
    
  $salesChannel=$jresponse->salesChannel;
  $total_subTotal=$jresponse->total->subTotal;
  $total_deliveryFee=$jresponse->total->deliveryFee;
  $total_benefits=$jresponse->total->benefits;
  $total_orderAmount=$jresponse->total->orderAmount;
  $total_additionalFees=$jresponse->total->additionalFees;
  
   $payments_prepaid=$jresponse->payments->prepaid;
   $payments_pending=$jresponse->payments->pending;
  
   $payments_methods_value=$jresponse->payments->methods[0]->value;
   $payments_methods_currency=$jresponse->payments->methods[0]->currency;
   $payments_methods_method=$jresponse->payments->methods[0]->method;        
   $payments_methods_type=$jresponse->payments->methods[0]->type;   
   $payments_methods_card_brand=$jresponse->payments->methods[0]->card->brand; 
   $payments_methods_prepaid=$jresponse->payments->methods[0]->prepaid;   
 
 
mais queria algo que quando tivesse mais de um produto item e adicionais usar foreach e for ou se tiver alguma outra sugestao
2 horas atrás, wash disse:

segue sugestão:
 

<?php
$jsonData = json_decode('{"items": [
        {
            "index": 1,
            "id": "26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId": "3fc4a0e3-f25e-46f7-a1e4-b66ecdb88152",
            "name": "PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)",
            "externalCode": "t02",
            "unit": "UN",
            "quantity": 1,
            "unitPrice": 0.00,
            "optionsPrice": 24.00,
            "totalPrice": 24.00,
            "options": [
                {
                    "index": 2,
                    "id": "a8b24880-b3f6-390a-8514-77bd7b2055aa",
                    "name": "Calabresa",
                    "externalCode": "c02-i002",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 20.00,
                    "addition": 0.00,
                    "price": 20.00
                },
                {
                    "index": 3,
                    "id": "893622be-1730-318e-a486-b2dd4fdfa87d",
                    "name": "Massa Fina + Borda Recheada",
                    "externalCode": "m02",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 4.00,
                    "addition": 0.00,
                    "price": 4.00
                }
            ],
            "price": 0.00
        },
        {
            "index": 4,
            "id": "b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId": "663ff728-af46-41ae-bb8d-4ffcc1b961e4",
            "name": "PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit": "GRAMS",
            "quantity": 1,
            "unitPrice": 10.00,
            "optionsPrice": 10.00,
            "totalPrice": 20.00,
            "options": [
                {
                    "index": 5,
                    "id": "d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name": "Laranja",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 10.00,
                    "addition": 0.00,
                    "price": 10.00
                }
            ],
            "price": 10.00
        },
        {
            "index": 6,
            "id": "e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId": "98de1e93-ba34-48bb-8f75-1b8c21e43fd1",
            "name": "PEDIDO DE TESTE - Sanduíche",
            "externalCode": "c01-i001",
            "unit": "GRAMS",
            "quantity": 1,
            "unitPrice": 20.00,
            "optionsPrice": 1.00,
            "totalPrice": 21.00,
            "options": [
                {
                    "index": 7,
                    "id": "1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name": "Complemento 1",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 1.00,
                    "addition": 0.00,
                    "price": 1.00
                },
                {
                    "index": 8,
                    "id": "42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name": "Complemento 6",
                    "unit": "UN",
                    "quantity": 1,
                    "unitPrice": 0.00,
                    "addition": 0.00,
                    "price": 0.00
                }
            ],
            "price": 20.00
        }
    ]}');

foreach ($jsonData->items as $v) {
  echo '<pre>$v: ';
  print_r($v);
  echo '</pre>';
}

pra ficar mais claro esse é o $response que recebo 

{
    "id""5e58177c-d8f2-41cf-9277-7fa742806f03",
    "delivery": {
        "mode""DEFAULT",
        "deliveredBy""MERCHANT",
        "deliveryDateTime""2022-12-30T19:05:40.933Z",
        "observations""Portão da casa/prédio",
        "deliveryAddress": {
            "streetName""PEDIDO DE TESTE - NÃO ENTREGAR - Ramal Bujari",
            "streetNumber""100",
            "formattedAddress""PEDIDO DE TESTE - NÃO ENTREGAR - Ramal Bujari, 100",
            "neighborhood""Bujari",
            "postalCode""00000000",
            "city""Bujari",
            "state""AC",
            "country""BR",
            "coordinates": {
                "latitude"-9.822081,
                "longitude"-67.948635
            }
        }
    },
    "orderType""DELIVERY",
    "orderTiming""IMMEDIATE",
    "displayId""0179",
    "createdAt""2022-12-30T18:15:40.933Z",
    "preparationStartDateTime""2022-12-30T18:15:40.933Z",
    "isTest"true,
    "merchant": {
        "id""b053eb1d-294a-4a42-806b-a46f5e6517b6",
        "name""Teste - PED NO WHATS"
    },
    "customer": {
        "id""932ab1fc-f076-4a41-b6d8-838ecf646cad",
        "name""PEDIDO DE TESTE - adriano rosa do prado",
        "phone": {
            "number""0800 007 0110",
            "localizer""28528522",
            "localizerExpiration""2022-12-30T21:15:40.933Z"
        },
        "ordersCountOnMerchant"0
    },
    "items": [
        {
            "index"1,
            "id""26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId""1da2022d-8e2c-4025-b095-34935203f239",
            "name""PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)",
            "externalCode""t02",
            "unit""UN",
            "quantity"1,
            "unitPrice"0.00,
            "optionsPrice"25.00,
            "totalPrice"25.00,
            "options": [
                {
                    "index"2,
                    "id""a478fba7-ebe9-38d1-8b71-8897f02d2e31",
                    "name""Portuguesa",
                    "externalCode""c02-i001",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"22.00,
                    "addition"0.00,
                    "price"22.00
                },
                {
                    "index"3,
                    "id""cc8831f7-f486-3d37-be6c-8d85c90920da",
                    "name""Massa Tradicional + Borda Recheada",
                    "externalCode""m01",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"3.00,
                    "addition"0.00,
                    "price"3.00
                }
            ],
            "price"0.00
        },
        {
            "index"4,
            "id""b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId""b369521e-0b21-449a-a754-3f5fbf3549a5",
            "name""PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"10.00,
            "optionsPrice"10.00,
            "totalPrice"20.00,
            "options": [
                {
                    "index"5,
                    "id""d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name""Laranja",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"10.00,
                    "addition"0.00,
                    "price"10.00
                }
            ],
            "price"10.00
        },
        {
            "index"6,
            "id""e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId""1361931c-ccc0-4889-b495-2f8319d08ec7",
            "name""PEDIDO DE TESTE - Sanduíche",
            "externalCode""c01-i001",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"20.00,
            "optionsPrice"1.00,
            "totalPrice"21.00,
            "options": [
                {
                    "index"7,
                    "id""1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name""Complemento 1",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"1.00,
                    "addition"0.00,
                    "price"1.00
                },
                {
                    "index"8,
                    "id""42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name""Complemento 6",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"0.00,
                    "addition"0.00,
                    "price"0.00
                }
            ],
            "price"20.00
        }
    ],
    "salesChannel""IFOOD",
    "total": {
        "subTotal"66.00,
        "deliveryFee"8.90,
        "benefits"0,
        "orderAmount"74.90,
        "additionalFees"0.00
    },
    "payments": {
        "prepaid"74.90,
        "pending"0,
        "methods": [
            {
                "value"74.90,
                "currency""BRL",
                "method""CREDIT",
                "type""ONLINE",
                "card": {
                    "brand""VISA"
                },
                "prepaid"true
            }
        ]
    }
}
 
nesse exemplo tenho 3 produtos em ITEM que são os index 1, 4 e 6 
e os adicionais dos produtos que são os index 2,3,5,7,8
 
que saber como pegar em um foreach e fazer tambem um for quando tiver adicionais ou não 
 
quando tem um produto apenas eu faco assim
 
$jresponse=json_decode($response);
  
  $id=$jresponse->id;
  $delivery_mode=$jresponse->delivery->mode;
  $delivery_deliveredBy=$jresponse->delivery->deliveredBy;
  $delivery_deliveryDateTime=$jresponse->delivery->deliveryDateTime;
  $delivery_observations=$jresponse->delivery->observations;
  
  $delivery_deliveryAddress_streetName=$jresponse->delivery->deliveryAddress->streetName;
  $delivery_deliveryAddress_streetNumber=$jresponse->delivery->deliveryAddress->streetNumber;
  $delivery_deliveryAddress_formattedAddress=$jresponse->delivery->deliveryAddress->formattedAddress;
  $delivery_deliveryAddress_neighborhood=$jresponse->delivery->deliveryAddress->neighborhood;
  $delivery_deliveryAddress_postalCode=$jresponse->delivery->deliveryAddress->postalCode;        
  $delivery_deliveryAddress_city=$jresponse->delivery->deliveryAddress->city;    
  $delivery_deliveryAddress_state=$jresponse->delivery->deliveryAddress->state;    
  $delivery_deliveryAddress_country=$jresponse->delivery->deliveryAddress->country;    
  
  $delivery_deliveryAddress_coordinates_latitude=$jresponse->delivery->deliveryAddress->coordinates->latitude;    
  $delivery_deliveryAddress_coordinates_longitude=$jresponse->delivery->deliveryAddress->coordinates->longitude;
    
  $orderType=$jresponse->orderType;
  $orderTiming=$jresponse->orderTiming;
  $displayId=$jresponse->displayId;
  $createdAt=$jresponse->createdAt;
  $preparationStartDateTime=$jresponse->preparationStartDateTime;
  $isTest=$jresponse->isTest;
  
  $merchant_id=$jresponse->merchant->id;
  $merchant_name=$jresponse->merchant->name;
  
  $customer_id=$jresponse->customer->id;
  $customer_name=$jresponse->customer->name;
  
  $customer_phone_number=$jresponse->customer->phone->number;
  $customer_phone_localizer=$jresponse->customer->phone->localizer;
  $customer_phone_localizerExpiration=$jresponse->customer->phone->localizerExpiration;
  
  $customer_ordersCountOnMerchant=$jresponse->customer->ordersCountOnMerchant;
    
  $items_index=$jresponse->items[0]->index;
  $items_id=$jresponse->items[0]->id;
  $items_uniqueId=$jresponse->items[0]->uniqueId;
  $items_name=$jresponse->items[0]->name;
    
  $items_index2=$jresponse->items[1]->index;
  $items_id2=$jresponse->items[1]->id;
  $items_uniqueId2=$jresponse->items[1]->uniqueId;
  $items_name2=$jresponse->items[1]->name;
  
  $items_index3=$jresponse->items[2]->index;
  $items_id3=$jresponse->items[2]->id;
  $items_uniqueId3=$jresponse->items[2]->uniqueId;
  $items_name3=$jresponse->items[2]->name;
  
    
  $salesChannel=$jresponse->salesChannel;
  $total_subTotal=$jresponse->total->subTotal;
  $total_deliveryFee=$jresponse->total->deliveryFee;
  $total_benefits=$jresponse->total->benefits;
  $total_orderAmount=$jresponse->total->orderAmount;
  $total_additionalFees=$jresponse->total->additionalFees;
  
   $payments_prepaid=$jresponse->payments->prepaid;
   $payments_pending=$jresponse->payments->pending;
  
   $payments_methods_value=$jresponse->payments->methods[0]->value;
   $payments_methods_currency=$jresponse->payments->methods[0]->currency;
   $payments_methods_method=$jresponse->payments->methods[0]->method;        
   $payments_methods_type=$jresponse->payments->methods[0]->type;   
   $payments_methods_card_brand=$jresponse->payments->methods[0]->card->brand; 
   $payments_methods_prepaid=$jresponse->payments->methods[0]->prepaid;   
 
 
mais queria algo que quando tivesse mais de um produto item e adicionais usar foreach e for ou se tiver alguma outra sugestao

 

6 horas atrás, Frank K Hosaka disse:

Isso daí é bem difícil, faz dois anos que eu tento resolver esse tipo de problema, e nunca consegui.
Você diz que isso daí é json, mas aqui no meu PC eu vejo apenas como string.
Eu tentei transformar o string em json, assim $json=json_decode($string), mas a resposta que eu tive foi null.
Durante dois anos eu desisti de resolver esse tipo de problema, mas hoje eu tive a coragem de pegar o editor de texto, e transformar tudo que é { em [, e tudo que é } em ], e tudo que é : em =>, ou seja, montei um array. Aí sim eu consegui fazer alguma coisa! Olha só:
 

<?php
 $adr=["items"=> [
        [   "index"=> 1,
            "id"=> "26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId"=> "3fc4a0e3-f25e-46f7-a1e4-b66ecdb88152",
            "name"=> "PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)","externalCode"=> "t02",
            "unit"=> "UN","quantity"=> 1,"unitPrice"=> 0.00,"optionsPrice"=> 24.00,
            "totalPrice"=> 24.00,
            "options"=> [
                [   "index"=> 2,"id"=> "a8b24880-b3f6-390a-8514-77bd7b2055aa",
                    "name"=> "Calabresa","externalCode"=> "c02-i002",
                    "unit"=> "UN","quantity"=> 1,"unitPrice"=> 20.00,
                    "addition"=> 0.00,"price"=> 20.00],
                [   "index"=> 3,"id"=> "893622be-1730-318e-a486-b2dd4fdfa87d",
                    "name"=> "Massa Fina + Borda Recheada","externalCode"=> "m02",
                    "unit"=> "UN","quantity"=> 1,"unitPrice"=> 4.00,
                    "addition"=> 0.00,"price"=> 4.00]],
            "price"=> 0.00],
        [   "index"=> 4,
            "id"=> "b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId"=> "663ff728-af46-41ae-bb8d-4ffcc1b961e4",
            "name"=> "PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit"=> "GRAMS","quantity"=> 1,"unitPrice"=> 10.00,"optionsPrice"=> 10.00,
            "totalPrice"=> 20.00,
            "options"=> [
                [   "index"=> 5,"id"=> "d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name"=> "Laranja",
                    "unit"=> "UN","quantity"=> 1,"unitPrice"=> 10.00,
                    "addition"=> 0.00,"price"=> 10.00]],
            "price"=> 10.00],
        [   "index"=> 6,
            "id"=> "e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId"=> "98de1e93-ba34-48bb-8f75-1b8c21e43fd1",
            "name"=> "PEDIDO DE TESTE - Sanduíche",
            "externalCode"=> "c01-i001",
            "unit"=> "GRAMS","quantity"=> 1,"unitPrice"=> 20.00,"optionsPrice"=> 1.00,
            "totalPrice"=> 21.00,
            "options"=> [
                [   "index"=> 7,
                    "id"=> "1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name"=> "Complemento 1",
                    "unit"=> "UN","quantity"=> 1,"unitPrice"=> 1.00,
                    "addition"=> 0.00,"price"=> 1.00],
                [   "index"=> 8,
                    "id"=> "42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name"=> "Complemento 6",
                    "unit"=> "UN","quantity"=> 1,"unitPrice"=> 0.00,
                    "addition"=> 0.00,"price"=> 0.00]],
            "price"=> 20.00]]];
//var_dump($adr);
foreach($adr as $a){
    foreach($a as $b){
        echo $b['name']."<br>";
    }
}
/* resultado: 
PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)
PEDIDO DE TESTE - Bebida teste 100 ml
PEDIDO DE TESTE - Sanduíche
*/


mas, no seu caso, se você tiver um json de verdade, acho que esse código vai funcionar

foreach($meujson as $items) {
	foreach($items as $item) {
		echo $item->name; }}

A minha sugestão é sempre trabalhar com a função var_dump(), esse é o melhor caminho para estudar o PHP, e estudar a função dd() no caso do Laravel.

pra ficar mais claro esse é o $response que recebo 

{
    "id""5e58177c-d8f2-41cf-9277-7fa742806f03",
    "delivery": {
        "mode""DEFAULT",
        "deliveredBy""MERCHANT",
        "deliveryDateTime""2022-12-30T19:05:40.933Z",
        "observations""Portão da casa/prédio",
        "deliveryAddress": {
            "streetName""PEDIDO DE TESTE - NÃO ENTREGAR - Ramal Bujari",
            "streetNumber""100",
            "formattedAddress""PEDIDO DE TESTE - NÃO ENTREGAR - Ramal Bujari, 100",
            "neighborhood""Bujari",
            "postalCode""00000000",
            "city""Bujari",
            "state""AC",
            "country""BR",
            "coordinates": {
                "latitude"-9.822081,
                "longitude"-67.948635
            }
        }
    },
    "orderType""DELIVERY",
    "orderTiming""IMMEDIATE",
    "displayId""0179",
    "createdAt""2022-12-30T18:15:40.933Z",
    "preparationStartDateTime""2022-12-30T18:15:40.933Z",
    "isTest"true,
    "merchant": {
        "id""b053eb1d-294a-4a42-806b-a46f5e6517b6",
        "name""Teste - PED NO WHATS"
    },
    "customer": {
        "id""932ab1fc-f076-4a41-b6d8-838ecf646cad",
        "name""PEDIDO DE TESTE - adriano rosa do prado",
        "phone": {
            "number""0800 007 0110",
            "localizer""28528522",
            "localizerExpiration""2022-12-30T21:15:40.933Z"
        },
        "ordersCountOnMerchant"0
    },
    "items": [
        {
            "index"1,
            "id""26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId""1da2022d-8e2c-4025-b095-34935203f239",
            "name""PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)",
            "externalCode""t02",
            "unit""UN",
            "quantity"1,
            "unitPrice"0.00,
            "optionsPrice"25.00,
            "totalPrice"25.00,
            "options": [
                {
                    "index"2,
                    "id""a478fba7-ebe9-38d1-8b71-8897f02d2e31",
                    "name""Portuguesa",
                    "externalCode""c02-i001",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"22.00,
                    "addition"0.00,
                    "price"22.00
                },
                {
                    "index"3,
                    "id""cc8831f7-f486-3d37-be6c-8d85c90920da",
                    "name""Massa Tradicional + Borda Recheada",
                    "externalCode""m01",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"3.00,
                    "addition"0.00,
                    "price"3.00
                }
            ],
            "price"0.00
        },
        {
            "index"4,
            "id""b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId""b369521e-0b21-449a-a754-3f5fbf3549a5",
            "name""PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"10.00,
            "optionsPrice"10.00,
            "totalPrice"20.00,
            "options": [
                {
                    "index"5,
                    "id""d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name""Laranja",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"10.00,
                    "addition"0.00,
                    "price"10.00
                }
            ],
            "price"10.00
        },
        {
            "index"6,
            "id""e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId""1361931c-ccc0-4889-b495-2f8319d08ec7",
            "name""PEDIDO DE TESTE - Sanduíche",
            "externalCode""c01-i001",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"20.00,
            "optionsPrice"1.00,
            "totalPrice"21.00,
            "options": [
                {
                    "index"7,
                    "id""1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name""Complemento 1",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"1.00,
                    "addition"0.00,
                    "price"1.00
                },
                {
                    "index"8,
                    "id""42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name""Complemento 6",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"0.00,
                    "addition"0.00,
                    "price"0.00
                }
            ],
            "price"20.00
        }
    ],
    "salesChannel""IFOOD",
    "total": {
        "subTotal"66.00,
        "deliveryFee"8.90,
        "benefits"0,
        "orderAmount"74.90,
        "additionalFees"0.00
    },
    "payments": {
        "prepaid"74.90,
        "pending"0,
        "methods": [
            {
                "value"74.90,
                "currency""BRL",
                "method""CREDIT",
                "type""ONLINE",
                "card": {
                    "brand""VISA"
                },
                "prepaid"true
            }
        ]
    }
}
 
nesse exemplo tenho 3 produtos em ITEM que são os index 1, 4 e 6 
e os adicionais dos produtos que são os index 2,3,5,7,8
 
que saber como pegar em um foreach e fazer tambem um for quando tiver adicionais ou não 
 
quando tem um produto apenas eu faco assim
 
$jresponse=json_decode($response);
  
  $id=$jresponse->id;
  $delivery_mode=$jresponse->delivery->mode;
  $delivery_deliveredBy=$jresponse->delivery->deliveredBy;
  $delivery_deliveryDateTime=$jresponse->delivery->deliveryDateTime;
  $delivery_observations=$jresponse->delivery->observations;
  
  $delivery_deliveryAddress_streetName=$jresponse->delivery->deliveryAddress->streetName;
  $delivery_deliveryAddress_streetNumber=$jresponse->delivery->deliveryAddress->streetNumber;
  $delivery_deliveryAddress_formattedAddress=$jresponse->delivery->deliveryAddress->formattedAddress;
  $delivery_deliveryAddress_neighborhood=$jresponse->delivery->deliveryAddress->neighborhood;
  $delivery_deliveryAddress_postalCode=$jresponse->delivery->deliveryAddress->postalCode;        
  $delivery_deliveryAddress_city=$jresponse->delivery->deliveryAddress->city;    
  $delivery_deliveryAddress_state=$jresponse->delivery->deliveryAddress->state;    
  $delivery_deliveryAddress_country=$jresponse->delivery->deliveryAddress->country;    
  
  $delivery_deliveryAddress_coordinates_latitude=$jresponse->delivery->deliveryAddress->coordinates->latitude;    
  $delivery_deliveryAddress_coordinates_longitude=$jresponse->delivery->deliveryAddress->coordinates->longitude;
    
  $orderType=$jresponse->orderType;
  $orderTiming=$jresponse->orderTiming;
  $displayId=$jresponse->displayId;
  $createdAt=$jresponse->createdAt;
  $preparationStartDateTime=$jresponse->preparationStartDateTime;
  $isTest=$jresponse->isTest;
  
  $merchant_id=$jresponse->merchant->id;
  $merchant_name=$jresponse->merchant->name;
  
  $customer_id=$jresponse->customer->id;
  $customer_name=$jresponse->customer->name;
  
  $customer_phone_number=$jresponse->customer->phone->number;
  $customer_phone_localizer=$jresponse->customer->phone->localizer;
  $customer_phone_localizerExpiration=$jresponse->customer->phone->localizerExpiration;
  
  $customer_ordersCountOnMerchant=$jresponse->customer->ordersCountOnMerchant;
    
  $items_index=$jresponse->items[0]->index;
  $items_id=$jresponse->items[0]->id;
  $items_uniqueId=$jresponse->items[0]->uniqueId;
  $items_name=$jresponse->items[0]->name;
    
  $items_index2=$jresponse->items[1]->index;
  $items_id2=$jresponse->items[1]->id;
  $items_uniqueId2=$jresponse->items[1]->uniqueId;
  $items_name2=$jresponse->items[1]->name;
  
  $items_index3=$jresponse->items[2]->index;
  $items_id3=$jresponse->items[2]->id;
  $items_uniqueId3=$jresponse->items[2]->uniqueId;
  $items_name3=$jresponse->items[2]->name;
  
    
  $salesChannel=$jresponse->salesChannel;
  $total_subTotal=$jresponse->total->subTotal;
  $total_deliveryFee=$jresponse->total->deliveryFee;
  $total_benefits=$jresponse->total->benefits;
  $total_orderAmount=$jresponse->total->orderAmount;
  $total_additionalFees=$jresponse->total->additionalFees;
  
   $payments_prepaid=$jresponse->payments->prepaid;
   $payments_pending=$jresponse->payments->pending;
  
   $payments_methods_value=$jresponse->payments->methods[0]->value;
   $payments_methods_currency=$jresponse->payments->methods[0]->currency;
   $payments_methods_method=$jresponse->payments->methods[0]->method;        
   $payments_methods_type=$jresponse->payments->methods[0]->type;   
   $payments_methods_card_brand=$jresponse->payments->methods[0]->card->brand; 
   $payments_methods_prepaid=$jresponse->payments->methods[0]->prepaid;   
 
 
mais queria algo que quando tivesse mais de um produto item e adicionais usar foreach e for ou se tiver alguma outra sugestao
Link para o comentário
Compartilhar em outros sites

  • 0
21 minutos atrás, adrsublimacao disse:

pra ficar mais claro esse é o $response que recebo 

{
    "id""5e58177c-d8f2-41cf-9277-7fa742806f03",
    "delivery": {
        "mode""DEFAULT",
        "deliveredBy""MERCHANT",
        "deliveryDateTime""2022-12-30T19:05:40.933Z",
        "observations""Portão da casa/prédio",
        "deliveryAddress": {
            "streetName""PEDIDO DE TESTE - NÃO ENTREGAR - Ramal Bujari",
            "streetNumber""100",
            "formattedAddress""PEDIDO DE TESTE - NÃO ENTREGAR - Ramal Bujari, 100",
            "neighborhood""Bujari",
            "postalCode""00000000",
            "city""Bujari",
            "state""AC",
            "country""BR",
            "coordinates": {
                "latitude"-9.822081,
                "longitude"-67.948635
            }
        }
    },
    "orderType""DELIVERY",
    "orderTiming""IMMEDIATE",
    "displayId""0179",
    "createdAt""2022-12-30T18:15:40.933Z",
    "preparationStartDateTime""2022-12-30T18:15:40.933Z",
    "isTest"true,
    "merchant": {
        "id""b053eb1d-294a-4a42-806b-a46f5e6517b6",
        "name""Teste - PED NO WHATS"
    },
    "customer": {
        "id""932ab1fc-f076-4a41-b6d8-838ecf646cad",
        "name""PEDIDO DE TESTE - adriano rosa do prado",
        "phone": {
            "number""0800 007 0110",
            "localizer""28528522",
            "localizerExpiration""2022-12-30T21:15:40.933Z"
        },
        "ordersCountOnMerchant"0
    },
    "items": [
        {
            "index"1,
            "id""26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId""1da2022d-8e2c-4025-b095-34935203f239",
            "name""PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)",
            "externalCode""t02",
            "unit""UN",
            "quantity"1,
            "unitPrice"0.00,
            "optionsPrice"25.00,
            "totalPrice"25.00,
            "options": [
                {
                    "index"2,
                    "id""a478fba7-ebe9-38d1-8b71-8897f02d2e31",
                    "name""Portuguesa",
                    "externalCode""c02-i001",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"22.00,
                    "addition"0.00,
                    "price"22.00
                },
                {
                    "index"3,
                    "id""cc8831f7-f486-3d37-be6c-8d85c90920da",
                    "name""Massa Tradicional + Borda Recheada",
                    "externalCode""m01",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"3.00,
                    "addition"0.00,
                    "price"3.00
                }
            ],
            "price"0.00
        },
        {
            "index"4,
            "id""b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId""b369521e-0b21-449a-a754-3f5fbf3549a5",
            "name""PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"10.00,
            "optionsPrice"10.00,
            "totalPrice"20.00,
            "options": [
                {
                    "index"5,
                    "id""d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name""Laranja",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"10.00,
                    "addition"0.00,
                    "price"10.00
                }
            ],
            "price"10.00
        },
        {
            "index"6,
            "id""e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId""1361931c-ccc0-4889-b495-2f8319d08ec7",
            "name""PEDIDO DE TESTE - Sanduíche",
            "externalCode""c01-i001",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"20.00,
            "optionsPrice"1.00,
            "totalPrice"21.00,
            "options": [
                {
                    "index"7,
                    "id""1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name""Complemento 1",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"1.00,
                    "addition"0.00,
                    "price"1.00
                },
                {
                    "index"8,
                    "id""42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name""Complemento 6",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"0.00,
                    "addition"0.00,
                    "price"0.00
                }
            ],
            "price"20.00
        }
    ],
    "salesChannel""IFOOD",
    "total": {
        "subTotal"66.00,
        "deliveryFee"8.90,
        "benefits"0,
        "orderAmount"74.90,
        "additionalFees"0.00
    },
    "payments": {
        "prepaid"74.90,
        "pending"0,
        "methods": [
            {
                "value"74.90,
                "currency""BRL",
                "method""CREDIT",
                "type""ONLINE",
                "card": {
                    "brand""VISA"
                },
                "prepaid"true
            }
        ]
    }
}
 
nesse exemplo tenho 3 produtos em ITEM que são os index 1, 4 e 6 
e os adicionais dos produtos que são os index 2,3,5,7,8
 
que saber como pegar em um foreach e fazer tambem um for quando tiver adicionais ou não 
 
quando tem um produto apenas eu faco assim
 
$jresponse=json_decode($response);
  
  $id=$jresponse->id;
  $delivery_mode=$jresponse->delivery->mode;
  $delivery_deliveredBy=$jresponse->delivery->deliveredBy;
  $delivery_deliveryDateTime=$jresponse->delivery->deliveryDateTime;
  $delivery_observations=$jresponse->delivery->observations;
  
  $delivery_deliveryAddress_streetName=$jresponse->delivery->deliveryAddress->streetName;
  $delivery_deliveryAddress_streetNumber=$jresponse->delivery->deliveryAddress->streetNumber;
  $delivery_deliveryAddress_formattedAddress=$jresponse->delivery->deliveryAddress->formattedAddress;
  $delivery_deliveryAddress_neighborhood=$jresponse->delivery->deliveryAddress->neighborhood;
  $delivery_deliveryAddress_postalCode=$jresponse->delivery->deliveryAddress->postalCode;        
  $delivery_deliveryAddress_city=$jresponse->delivery->deliveryAddress->city;    
  $delivery_deliveryAddress_state=$jresponse->delivery->deliveryAddress->state;    
  $delivery_deliveryAddress_country=$jresponse->delivery->deliveryAddress->country;    
  
  $delivery_deliveryAddress_coordinates_latitude=$jresponse->delivery->deliveryAddress->coordinates->latitude;    
  $delivery_deliveryAddress_coordinates_longitude=$jresponse->delivery->deliveryAddress->coordinates->longitude;
    
  $orderType=$jresponse->orderType;
  $orderTiming=$jresponse->orderTiming;
  $displayId=$jresponse->displayId;
  $createdAt=$jresponse->createdAt;
  $preparationStartDateTime=$jresponse->preparationStartDateTime;
  $isTest=$jresponse->isTest;
  
  $merchant_id=$jresponse->merchant->id;
  $merchant_name=$jresponse->merchant->name;
  
  $customer_id=$jresponse->customer->id;
  $customer_name=$jresponse->customer->name;
  
  $customer_phone_number=$jresponse->customer->phone->number;
  $customer_phone_localizer=$jresponse->customer->phone->localizer;
  $customer_phone_localizerExpiration=$jresponse->customer->phone->localizerExpiration;
  
  $customer_ordersCountOnMerchant=$jresponse->customer->ordersCountOnMerchant;
    
  $items_index=$jresponse->items[0]->index;
  $items_id=$jresponse->items[0]->id;
  $items_uniqueId=$jresponse->items[0]->uniqueId;
  $items_name=$jresponse->items[0]->name;
    
  $items_index2=$jresponse->items[1]->index;
  $items_id2=$jresponse->items[1]->id;
  $items_uniqueId2=$jresponse->items[1]->uniqueId;
  $items_name2=$jresponse->items[1]->name;
  
  $items_index3=$jresponse->items[2]->index;
  $items_id3=$jresponse->items[2]->id;
  $items_uniqueId3=$jresponse->items[2]->uniqueId;
  $items_name3=$jresponse->items[2]->name;
  
    
  $salesChannel=$jresponse->salesChannel;
  $total_subTotal=$jresponse->total->subTotal;
  $total_deliveryFee=$jresponse->total->deliveryFee;
  $total_benefits=$jresponse->total->benefits;
  $total_orderAmount=$jresponse->total->orderAmount;
  $total_additionalFees=$jresponse->total->additionalFees;
  
   $payments_prepaid=$jresponse->payments->prepaid;
   $payments_pending=$jresponse->payments->pending;
  
   $payments_methods_value=$jresponse->payments->methods[0]->value;
   $payments_methods_currency=$jresponse->payments->methods[0]->currency;
   $payments_methods_method=$jresponse->payments->methods[0]->method;        
   $payments_methods_type=$jresponse->payments->methods[0]->type;   
   $payments_methods_card_brand=$jresponse->payments->methods[0]->card->brand; 
   $payments_methods_prepaid=$jresponse->payments->methods[0]->prepaid;   
 
 
mais queria algo que quando tivesse mais de um produto item e adicionais usar foreach e for ou se tiver alguma outra sugestao

 

pra ficar mais claro esse é o $response que recebo 

{
    "id""5e58177c-d8f2-41cf-9277-7fa742806f03",
    "delivery": {
        "mode""DEFAULT",
        "deliveredBy""MERCHANT",
        "deliveryDateTime""2022-12-30T19:05:40.933Z",
        "observations""Portão da casa/prédio",
        "deliveryAddress": {
            "streetName""PEDIDO DE TESTE - NÃO ENTREGAR - Ramal Bujari",
            "streetNumber""100",
            "formattedAddress""PEDIDO DE TESTE - NÃO ENTREGAR - Ramal Bujari, 100",
            "neighborhood""Bujari",
            "postalCode""00000000",
            "city""Bujari",
            "state""AC",
            "country""BR",
            "coordinates": {
                "latitude"-9.822081,
                "longitude"-67.948635
            }
        }
    },
    "orderType""DELIVERY",
    "orderTiming""IMMEDIATE",
    "displayId""0179",
    "createdAt""2022-12-30T18:15:40.933Z",
    "preparationStartDateTime""2022-12-30T18:15:40.933Z",
    "isTest"true,
    "merchant": {
        "id""b053eb1d-294a-4a42-806b-a46f5e6517b6",
        "name""Teste - PED NO WHATS"
    },
    "customer": {
        "id""932ab1fc-f076-4a41-b6d8-838ecf646cad",
        "name""PEDIDO DE TESTE - adriano rosa do prado",
        "phone": {
            "number""0800 007 0110",
            "localizer""28528522",
            "localizerExpiration""2022-12-30T21:15:40.933Z"
        },
        "ordersCountOnMerchant"0
    },
    "items": [
        {
            "index"1,
            "id""26845888-7547-3346-800d-9e3ae0353804",
            "uniqueId""1da2022d-8e2c-4025-b095-34935203f239",
            "name""PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)",
            "externalCode""t02",
            "unit""UN",
            "quantity"1,
            "unitPrice"0.00,
            "optionsPrice"25.00,
            "totalPrice"25.00,
            "options": [
                {
                    "index"2,
                    "id""a478fba7-ebe9-38d1-8b71-8897f02d2e31",
                    "name""Portuguesa",
                    "externalCode""c02-i001",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"22.00,
                    "addition"0.00,
                    "price"22.00
                },
                {
                    "index"3,
                    "id""cc8831f7-f486-3d37-be6c-8d85c90920da",
                    "name""Massa Tradicional + Borda Recheada",
                    "externalCode""m01",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"3.00,
                    "addition"0.00,
                    "price"3.00
                }
            ],
            "price"0.00
        },
        {
            "index"4,
            "id""b78d3091-cead-4c12-a06a-1440b3176c77",
            "uniqueId""b369521e-0b21-449a-a754-3f5fbf3549a5",
            "name""PEDIDO DE TESTE - Bebida teste 100 ml",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"10.00,
            "optionsPrice"10.00,
            "totalPrice"20.00,
            "options": [
                {
                    "index"5,
                    "id""d4603ddd-3519-4fcb-a450-29e06ab5d9ff",
                    "name""Laranja",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"10.00,
                    "addition"0.00,
                    "price"10.00
                }
            ],
            "price"10.00
        },
        {
            "index"6,
            "id""e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c",
            "uniqueId""1361931c-ccc0-4889-b495-2f8319d08ec7",
            "name""PEDIDO DE TESTE - Sanduíche",
            "externalCode""c01-i001",
            "unit""GRAMS",
            "quantity"1,
            "unitPrice"20.00,
            "optionsPrice"1.00,
            "totalPrice"21.00,
            "options": [
                {
                    "index"7,
                    "id""1101565b-dc4b-44d3-9e82-b7c4b07397f3",
                    "name""Complemento 1",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"1.00,
                    "addition"0.00,
                    "price"1.00
                },
                {
                    "index"8,
                    "id""42e8a1f3-6f3e-4370-9223-05464f5be701",
                    "name""Complemento 6",
                    "unit""UN",
                    "quantity"1,
                    "unitPrice"0.00,
                    "addition"0.00,
                    "price"0.00
                }
            ],
            "price"20.00
        }
    ],
    "salesChannel""IFOOD",
    "total": {
        "subTotal"66.00,
        "deliveryFee"8.90,
        "benefits"0,
        "orderAmount"74.90,
        "additionalFees"0.00
    },
    "payments": {
        "prepaid"74.90,
        "pending"0,
        "methods": [
            {
                "value"74.90,
                "currency""BRL",
                "method""CREDIT",
                "type""ONLINE",
                "card": {
                    "brand""VISA"
                },
                "prepaid"true
            }
        ]
    }
}
 
nesse exemplo tenho 3 produtos em ITEM que são os index 1, 4 e 6 
e os adicionais dos produtos que são os index 2,3,5,7,8
 
que saber como pegar em um foreach e fazer tambem um for quando tiver adicionais ou não 
 
quando tem um produto apenas eu faco assim
 
$jresponse=json_decode($response);
  
  $id=$jresponse->id;
  $delivery_mode=$jresponse->delivery->mode;
  $delivery_deliveredBy=$jresponse->delivery->deliveredBy;
  $delivery_deliveryDateTime=$jresponse->delivery->deliveryDateTime;
  $delivery_observations=$jresponse->delivery->observations;
  
  $delivery_deliveryAddress_streetName=$jresponse->delivery->deliveryAddress->streetName;
  $delivery_deliveryAddress_streetNumber=$jresponse->delivery->deliveryAddress->streetNumber;
  $delivery_deliveryAddress_formattedAddress=$jresponse->delivery->deliveryAddress->formattedAddress;
  $delivery_deliveryAddress_neighborhood=$jresponse->delivery->deliveryAddress->neighborhood;
  $delivery_deliveryAddress_postalCode=$jresponse->delivery->deliveryAddress->postalCode;        
  $delivery_deliveryAddress_city=$jresponse->delivery->deliveryAddress->city;    
  $delivery_deliveryAddress_state=$jresponse->delivery->deliveryAddress->state;    
  $delivery_deliveryAddress_country=$jresponse->delivery->deliveryAddress->country;    
  
  $delivery_deliveryAddress_coordinates_latitude=$jresponse->delivery->deliveryAddress->coordinates->latitude;    
  $delivery_deliveryAddress_coordinates_longitude=$jresponse->delivery->deliveryAddress->coordinates->longitude;
    
  $orderType=$jresponse->orderType;
  $orderTiming=$jresponse->orderTiming;
  $displayId=$jresponse->displayId;
  $createdAt=$jresponse->createdAt;
  $preparationStartDateTime=$jresponse->preparationStartDateTime;
  $isTest=$jresponse->isTest;
  
  $merchant_id=$jresponse->merchant->id;
  $merchant_name=$jresponse->merchant->name;
  
  $customer_id=$jresponse->customer->id;
  $customer_name=$jresponse->customer->name;
  
  $customer_phone_number=$jresponse->customer->phone->number;
  $customer_phone_localizer=$jresponse->customer->phone->localizer;
  $customer_phone_localizerExpiration=$jresponse->customer->phone->localizerExpiration;
  
  $customer_ordersCountOnMerchant=$jresponse->customer->ordersCountOnMerchant;
    
  $items_index=$jresponse->items[0]->index;
  $items_id=$jresponse->items[0]->id;
  $items_uniqueId=$jresponse->items[0]->uniqueId;
  $items_name=$jresponse->items[0]->name;
    
  $items_index2=$jresponse->items[1]->index;
  $items_id2=$jresponse->items[1]->id;
  $items_uniqueId2=$jresponse->items[1]->uniqueId;
  $items_name2=$jresponse->items[1]->name;
  
  $items_index3=$jresponse->items[2]->index;
  $items_id3=$jresponse->items[2]->id;
  $items_uniqueId3=$jresponse->items[2]->uniqueId;
  $items_name3=$jresponse->items[2]->name;
  
    
  $salesChannel=$jresponse->salesChannel;
  $total_subTotal=$jresponse->total->subTotal;
  $total_deliveryFee=$jresponse->total->deliveryFee;
  $total_benefits=$jresponse->total->benefits;
  $total_orderAmount=$jresponse->total->orderAmount;
  $total_additionalFees=$jresponse->total->additionalFees;
  
   $payments_prepaid=$jresponse->payments->prepaid;
   $payments_pending=$jresponse->payments->pending;
  
   $payments_methods_value=$jresponse->payments->methods[0]->value;
   $payments_methods_currency=$jresponse->payments->methods[0]->currency;
   $payments_methods_method=$jresponse->payments->methods[0]->method;        
   $payments_methods_type=$jresponse->payments->methods[0]->type;   
   $payments_methods_card_brand=$jresponse->payments->methods[0]->card->brand; 
   $payments_methods_prepaid=$jresponse->payments->methods[0]->prepaid;   
 
 
mais queria algo que quando tivesse mais de um produto item e adicionais usar foreach e for ou se tiver alguma outra sugestao
foreach($meujson as $items) {
	foreach($items as $item) {
		echo $item->name; }}

esse codigo funcionou certinho

mais não pegou os adicionais que estao no index 2,3 vinculado ao index 1 

no index 5 vinculado ao index 4

 no index 7, 8 vinculado ao index 6

Link para o comentário
Compartilhar em outros sites

  • 0
37 minutos atrás, adrsublimacao disse:
foreach($meujson as $items) {
	foreach($items as $item) {
		echo $item->name; }}

esse codigo funcionou certinho

mais não pegou os adicionais que estao no index 2,3 vinculado ao index 1 

no index 5 vinculado ao index 4

 no index 7, 8 vinculado ao index 6

mudei para esse codigo mais ai pega os adicionais do primeiro produto em todos os produtos dai

foreach($jresponse->items as $item) {
      
      echo $item->id;
        echo $item->name;    
        echo $item->totalPrice;
      
          foreach($jresponse->items[0]->options as $opcionais) {     
      
          echo $opcionais->id;
        echo $opcionais->name;    
        echo $opcionais->totalPrice;
             

    }
  }

recebi isso com esse codigo

26845888-7547-3346-800d-9e3ae0353804
PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)
22

a478fba7-ebe9-38d1-8b71-8897f02d2e31
Portuguesa
2

f6f8d51-27c4-3d28-9a39-7817fc6f090c
Massa Tradicional + Borda Tradicional

b78d3091-cead-4c12-a06a-1440b3176c77
PEDIDO DE TESTE - Bebida teste 100 ml
20

a478fba7-ebe9-38d1-8b71-8897f02d2e31
Portuguesa
2

f6f8d51-27c4-3d28-9a39-7817fc6f090c
Massa Tradicional + Borda Tradicional

e8f9f490-2a56-4bb8-a9d3-5e9b72fe027c

PEDIDO DE TESTE - Sanduíche
21
a478fba7-ebe9-38d1-8b71-8897f02d2e31
Portuguesa
2

f6f8d51-27c4-3d28-9a39-7817fc6f090c
Massa Tradicional + Borda Tradicional

 

esse rcodigos pega so os opcionais do primeiro produto $jresponse->items[0]->options

no caso teria q fazer assim

$jresponse->items[0]->options

$jresponse->items[1]->options

$jresponse->items[2]->options

e assim por diante ate varer todos os opcionais de cada produto

 

deu certo consegui aqui 

muito obrigado

 

Link para o comentário
Compartilhar em outros sites

  • 0
5 horas atrás, Frank K Hosaka disse:

Bom, eu não sou especialista como informei logo no começo da minha resposta. Tudo que eu faço é tentativa e erro, já esse último response que você passou foi bem fácil de trabalhar.

Eu não tenho como pegar o seu json, mas apenas o string que aparece na tela. Peguei o string e...

 

<?php
$string= (aqui eu copiei o response que foi divulgado na mensagem)
$json=json_decode($string);
echo "Pedido ".$json->id."<br>";
echo "Cliente ".$json->customer->name."<br>";
echo "itens ".count($json->items)."<p>";
$items=$json->items;
foreach($items as $item){
    echo $item->name."<br>";
    echo "adicionais ".count($item->options)."<br>";
    foreach($item->options as $option){
        echo $option->name."<br>";
    } echo "<p>";
    }
      
Resultado:
Pedido 5e58177c-d8f2-41cf-9277-7fa742806f03
Cliente PEDIDO DE TESTE - adriano rosa do prado
itens 3
PEDIDO DE TESTE - MÉDIA (6 PEDAÇOS)
adicionais 2
Portuguesa
Massa Tradicional + Borda Recheada

PEDIDO DE TESTE - Bebida teste 100 ml
adicionais 1
Laranja

PEDIDO DE TESTE - Sanduíche
adicionais 2
Complemento 1
Complemento 6

Ou seja, eu só faço tentativa e erro. Como o seu response é um json, você não precisa da primeira linha desse código, apenas eu.

Show de bola seu código parabéns e muito obrigado

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,3k
    • Posts
      652,3k
×
×
  • Criar Novo...