B
B
Bravis20182018-04-04 18:19:15
PHP
Bravis2018, 2018-04-04 18:19:15

How to print all elements of an array from JSON?

How to loop through all elements of listProduct array from json in php?

{"idUser":1,"listProduct":[{"amount":5,"idProduct":2,"price":0.0,"weight":0.0}],"size":1}

The json itself comes via a POST request.
<?php
$data = file_get_contents('php://input');
$json_data = json_decode($data , true);
$mysqli = new mysqli('localhost', 'db_user', 'db_pass', 'db_name');
$mysqli->set_charset('utf8');
  if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  if ($data == "" || empty($json_data['idUser']) || empty($json_data['listProduct'])) {
  $response = array('status' => false, 'message' => 'Failure');    
  } else {
  $idUser = (int) $json_data['idUser'];
  foreach ($json_data as $key => $value) {
    		$message = $message." ". $value["amount"] . ", " . $value["idProduct"] . "<br>";
  }
 }
...
 }
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arman, 2018-04-04
@Bravis2018

foreach ($json_data['listProduct'] as $key => $value) {

S
Sanovskiy, 2018-04-04
@Sanovskiy

But why such a perversion? There are also $_GET , $_POST and $_REQUEST

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question