I
I
Ibishka2020-04-27 22:55:14
PHP
Ibishka, 2020-04-27 22:55:14

How to take value from PHP object?

JSON is sent from js. stringify([2:3,4:5,6:8])
2 is the id 3 is the number.

$cartGoods = json_decode(file_get_contents('php://input'), true);
$goodsId = implode(",", array_map(
  function ($e) {
    return array_keys($e)[0];
  },
  $cartGoods
));
$goods = mysqli_query($connection, "SELECT * FROM `goods` WHERE `id` in ($goodsId)");

while ($item = mysqli_fetch_assoc($goods)) {
  foreach ($cartGoods as $obj) {
    $count = $obj[$item['id']];
  }
  echo $count;
}
?>

Why is it displayed 1 time despite the fact that 3 elements come from the database, etc.?
I need to know the id to pull the quantity from the cartGoods array. What is the problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Deyashkin, 2020-04-28
@Lobotomist

Personally, I am stopped from answering by the abundance of code that cannot be reproduced without your database and input data.
I advise you to isolate the problematic section of the code and leave only it in the question, and so that you can run it and observe the result that confuses you with your own eyes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question