How to add a new key value pair to each object in a PHP multi-dimensional array?

Solution:
You add an element to an array simply by assigning to the index.
foreach ($array as $i => $element) {
$array[$i]['liked'] = get_like($element);
}
You can also use a reference variable in theforeach
loop:
foreach ($array as $i => &$element) {
$element['liked'] = get_like($element);
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.