I have an array in the following format;
// echo '<pre>' . var_export($this->viewableFields, true) . '</pre>';
array (
0 =>
(object) array(
'formId' => '4',
'userId' => '7'
),
1 =>
(object) array(
'formId' => '4',
'userId' => '4'
)
)
I need to amend the data and add another key/value to this array. I need to use the userId
value from the array, query a MySQL database and return the value. I need to do this for each array element.
So for each array element I want to run a query like;
SELECT group from users WHERE userId = [userId in array]
I then want to add this value to the array, the final array should look like this;
array (
0 =>
(object) array(
'formId' => '4',
'userId' => '7',
'group' => 'Registered'
),
1 =>
(object) array(
'formId' => '4',
'userId' => '4',
'group' => 'Admin'
)
)
I know I can add an additional value to the array elements by using array_walk
, like this;
array_walk($this->viewableFields, function(&$arr) {
$arr->group = 'Registered';
});
I'm not sure how to retrieve the values from the database though and insert into the existing array.
How can I achieve this?