I have this data that I send by postman
{
"commerce_id": 1,
"interests": {
"question": 1,
"answer": 1
}
}
and this is my code in php;
function storeInterst($interestsData, $commerce_id)
{
try {
$interests = [];
foreach ($interestsData as $interest) {
echo $commerce_id;
echo $interest['question'] . "\n";
echo $interest['answer'] . "\n";
$interests[] = $this->commerceInterest->setDataRegister($interest['answer'], $commerce_id, $interest['question']);
}
echo "hola";
die;
$this->commerceInterest->insert($interests);
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
}
}
The thing is that the function does not print anything from the foreach and I know that the data is entering because the variable, when I print it as $interestsData, votes the data for me if I specify $interestsData["answer"]
Seeing your previous question I wonder if the answer was correct, because in this one you try to traverse an object as an array when it is not.
Try changing this:
For this:
so that, if there is more data, one day it can be like this:
and thus have an actual array that you can iterate through in that foreach that you do later.
By the way, it doesn't look like you're getting the json in your question, but for it to work with your current code, where it represents that the objects have been converted to an array, you'll need to use the parameter
true
while using json_decode() to convert it everything to associative arrays, something like this: