0

I have an array in this format;

// echo '<pre>' . var_export($this->viewableFields, true) . '</pre>';

array (
  0 => 
  (object) array(
     'formId' => '4',
     'componentId' => '-7',
     'viewable' => '1',
     'searchable' => '0',
     'editable' => '0'
  ),
  1 => 
  (object) array(
     'formId' => '4',
     'componentId' => '-4',
     'viewable' => '1',
     'searchable' => '1',
     'editable' => '0'
  )
)

I am selecting some data from a mysql database and I need to add this to the array. I want to add group = Registered, so the output will look like this;

// echo '<pre>' . var_export($this->viewableFields, true) . '</pre>';

array (
  0 => 
  (object) array(
     'formId' => '4',
     'componentId' => '-7',
     'viewable' => '1',
     'searchable' => '0',
     'editable' => '0',
     'group' => 'Registered'
  ),
  1 => 
  (object) array(
     'formId' => '4',
     'componentId' => '-4',
     'viewable' => '1',
     'searchable' => '1',
     'editable' => '0',
     'group' => 'Registered'
  )
)

I have tried the following but it simply creates another item at the end of the array (number [2]).

$group = (object) array(
    'group' => 'Registered'
);

array_push($this->viewableFields, $group);

Note, I can't modify the SQL.

0

1 Answer 1

2

Assuming $this->viewableFields is your wider array, you can use array_walk() to modify the array in-situ.

array_walk($this->viewableFields, function(&$arr) {
    $arr->group = 'Registered';
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.