'Adding columns to existing php arrays
Using PHP let's assume that I have successfully read a record from a MySQL table using the fetch_object method and I am holding the row data in a variable call $output:
while($row = $result->fetch_object())
{
$output[] = $row;
}
If I wanted to add two additional fields: "cls" and "parentID" to $output as if they were apart of $row, how would I accomplish this? Thanks!
Solution 1:[1]
Since you changed the code snippet in your question, try this instead now (updated version):
while(...) {
$row->cls = ...;
$row->parentID = ...;
$output[] = $row;
}
Solution 2:[2]
$myArray=array_merge($myArray,$myAddArray);
https://www.php.net/manual/en/function.array-merge.php
or use array_push()
apply it in the foreach/while loop for each row.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ledahu |
