adrsublimacao Postado Novembro 25, 2023 Denunciar Share Postado Novembro 25, 2023 {"paging":{"offset":0,"limit":100,"total":37},"results":[{"reason":"Plano Fidelidade 2","status":"active","subscribed":0,"back_url":"https://webhook.site/26b1e008-b05b-4b7a-a767-29bc21866c04","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebed70018bff3b0a600e2c","date_created":"2023-11-23T22:49:01.280-04:00","id":"2c9380848bebed70018bff3b0a600e2c","last_modified":"2023-11-23T23:05:33.382-04:00","application_id":7489360988572536}, {"reason":"Plano Fidelidade","status":"active","subscribed":0,"back_url":"https://pednowhats.com.br","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebed70018bff38e68d0e2a","date_created":"2023-11-23T22:46:41.037-04:00","id":"2c9380848bebed70018bff38e68d0e2a","last_modified":"2023-11-23T22:46:41.037-04:00","application_id":7489360988572536}, {"reason":"Plano Fidelidade","status":"active","subscribed":0,"back_url":"https://seu.site.com.br","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebedbc018c02446c111018","date_created":"2023-11-24T12:58:07.761-04:00","id":"2c9380848bebedbc018c02446c111018","last_modified":"2023-11-24T12:58:07.761-04:00","application_id":7489360988572536}, {"reason":"Plano Fidelidade","status":"active","subscribed":0,"back_url":"https://seu.site.com.br","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebed70018c02451f6c0f9c","date_created":"2023-11-24T12:58:53.676-04:00","id":"2c9380848bebed70018c02451f6c0f9c","last_modified":"2023-11-24T12:58:53.676-04:00","application_id":7489360988572536}]} boa noite como faço um foreach nessa string e salvo no banco o id reason transaction_amount usando esse salvamento abaixo mais salvando os 4 planos acima $CadastrarPlano= $connect->query("INSERT INTO plano (id, reason, transaction_amount) VALUES ('$id','$reason','$transaction_amount')"); Citar Link para o comentário Compartilhar em outros sites More sharing options...
1 wash Postado Novembro 25, 2023 Denunciar Share Postado Novembro 25, 2023 (editado) Olá! Segue sugestão. Considerando que sua $string seja um json válido: // decodificar a string como array $phpArray = json_decode($string, true); // data to record $dataToRecord = []; foreach($phpArray['results'] as $v) { // desestrutura as variáveis desejadas ['id' => $id, 'reason' => $reason, 'auto_recurring' => ['transaction_amount' => $transactionAmount]] = $v; // guarda em $dataToRecord $dataToRecord[] = "('$id','$reason',$transactionAmount)"; } // prepara a query de inserção $queryToInsert = 'INSERT INTO plano (id, reason, transaction_amount) VALUES '.implode(', ', $dataToRecord); // exibe na tela var_dump($queryToInsert); Editado Novembro 25, 2023 por wash Citar Link para o comentário Compartilhar em outros sites More sharing options...
1 Frank K Hosaka Postado Novembro 25, 2023 Denunciar Share Postado Novembro 25, 2023 (editado) <?php $string='{"paging":{"offset":0,"limit":100,"total":37},"results":[ {"reason":"Plano Fidelidade 2","status":"active","subscribed":0,"back_url":"https://webhook.site/26b1e008-b05b-4b7a-a767-29bc21866c04","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebed70018bff3b0a600e2c","date_created":"2023-11-23T22:49:01.280-04:00","id":"2c9380848bebed70018bff3b0a600e2c","last_modified":"2023-11-23T23:05:33.382-04:00","application_id":7489360988572536}, {"reason":"Plano Fidelidade","status":"active","subscribed":0,"back_url":"https://pednowhats.com.br","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebed70018bff38e68d0e2a","date_created":"2023-11-23T22:46:41.037-04:00","id":"2c9380848bebed70018bff38e68d0e2a","last_modified":"2023-11-23T22:46:41.037-04:00","application_id":7489360988572536}, {"reason":"Plano Fidelidade","status":"active","subscribed":0,"back_url":"https://seu.site.com.br","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebedbc018c02446c111018","date_created":"2023-11-24T12:58:07.761-04:00","id":"2c9380848bebedbc018c02446c111018","last_modified":"2023-11-24T12:58:07.761-04:00","application_id":7489360988572536}, {"reason":"Plano Fidelidade","status":"active","subscribed":0,"back_url":"https://seu.site.com.br","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebed70018c02451f6c0f9c","date_created":"2023-11-24T12:58:53.676-04:00","id":"2c9380848bebed70018c02451f6c0f9c","last_modified":"2023-11-24T12:58:53.676-04:00","application_id":7489360988572536}]}'; $js=json_decode($string); $results=$js->results; $values=[]; foreach($results as $result){ $amount=$result->auto_recurring->transaction_amount; $values[]="('$result->id','$result->reason',$amount)";} $connect=new mysqli("localhost","root","","laravel"); foreach($values as $value){ $connect->query ("insert into plano (id,reason,transaction_amount) values $value");} Usei a seguinte tabela: CREATE TABLE `plano` ( `id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `reason` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `transaction_amount` int DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci Editado Novembro 25, 2023 por Frank K Hosaka Citar Link para o comentário Compartilhar em outros sites More sharing options...
0 adrsublimacao Postado Novembro 25, 2023 Autor Denunciar Share Postado Novembro 25, 2023 deu certo as duas formas muito obrigado Citar Link para o comentário Compartilhar em outros sites More sharing options...
Pergunta
adrsublimacao
{"paging":{"offset":0,"limit":100,"total":37},"results":[{"reason":"Plano Fidelidade 2","status":"active","subscribed":0,"back_url":"https://webhook.site/26b1e008-b05b-4b7a-a767-29bc21866c04","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebed70018bff3b0a600e2c","date_created":"2023-11-23T22:49:01.280-04:00","id":"2c9380848bebed70018bff3b0a600e2c","last_modified":"2023-11-23T23:05:33.382-04:00","application_id":7489360988572536},
{"reason":"Plano Fidelidade","status":"active","subscribed":0,"back_url":"https://pednowhats.com.br","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebed70018bff38e68d0e2a","date_created":"2023-11-23T22:46:41.037-04:00","id":"2c9380848bebed70018bff38e68d0e2a","last_modified":"2023-11-23T22:46:41.037-04:00","application_id":7489360988572536},
{"reason":"Plano Fidelidade","status":"active","subscribed":0,"back_url":"https://seu.site.com.br","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebedbc018c02446c111018","date_created":"2023-11-24T12:58:07.761-04:00","id":"2c9380848bebedbc018c02446c111018","last_modified":"2023-11-24T12:58:07.761-04:00","application_id":7489360988572536},
{"reason":"Plano Fidelidade","status":"active","subscribed":0,"back_url":"https://seu.site.com.br","auto_recurring":{"frequency":1,"repetitions":12,"currency_id":"BRL","transaction_amount":10.0,"frequency_type":"months","free_trial":{"frequency":1,"frequency_type":"months"},"billing_day":10},"collector_id":147453588,"init_point":"https://www.mercadopago.com.br/subscriptions/checkout?preapproval_plan_id=2c9380848bebed70018c02451f6c0f9c","date_created":"2023-11-24T12:58:53.676-04:00","id":"2c9380848bebed70018c02451f6c0f9c","last_modified":"2023-11-24T12:58:53.676-04:00","application_id":7489360988572536}]}
boa noite como faço um foreach nessa string e salvo no banco o
id
reason
transaction_amount
usando esse salvamento abaixo mais salvando os 4 planos acima
Link para o comentário
Compartilhar em outros sites
3 respostass a esta questão
Posts Recomendados
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.