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.
Try $db_data->new_value ='prashant';
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';
}
You have to create a new stdClass object:
$tmp = new stdClass;
$tmp->new_value = 'prashant';
$db_data[] = $tmp;