1

I have an array which I am getting from my database. For instance:

$db_record = $this->search_model->search_Employee();
foreach ($db_record as $db_data) {
    echo $db_data->phone_number;
}

In above example I am able to fetch the record without any issue, but I want to add other data in $db_data afterwards.

foreach ($db_record as $db_data) {
    echo $db_data->phone_number;
    $db_data[]['new_value']='prashant';
}

but $db_data[]['new_value']='prashant'; throwing below error

Cannot use object of type stdClass as array in

It will great if someone can help me as struggeling more than 5 hours.

Ronni Egeriis Persson
  • 2,209
  • 1
  • 21
  • 43
prashant thakre
  • 5,061
  • 3
  • 26
  • 39

4 Answers4

6

Try $db_data->new_value ='prashant';

Valentin Rodygin
  • 864
  • 5
  • 12
3

You have to create a new stdClass object:

$tmp = new stdClass;
$tmp->new_value = 'prashant';
$db_data[] = $tmp;
luator
  • 4,769
  • 3
  • 30
  • 51
0
 foreach ($agent as $key => $value) {

   $value->any = 'ssds';
}

try to use the $value instead key and object

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
-1

Your $db_data is an object you can't use them like an array. Yout have to add a property to your object and init them with an empty array.

Example:

foreach ($db_record as $db_data) {
    echo $db_data->phone_number;
    $db_data->empty_array=array();
    $db_data->empty_array['new_value']='prashant';
}