bryguy bryguy - 6 months ago 8
MySQL Question

Is it possible to reassign created_at key in a PHP associative array?

// I am pulling product reviews from the database

$reviews = $this->Product->get_product_reviews_by_id($info);


// then iterating over them, formatting the date and attempting to reassign the created_at key

foreach($reviews as $r) {
$date = explode(" ", $r['created_at']);
$date = $date[0];
$time = date_format(date_create($date), "D F d, Y");
$r['created_at'] = $time;
}


// var dumping $time displays the desired date format, however, I have not been able to reassign/attach $time as the new value for display purposes on the front end. So the main question here is, can I overwrite the initial value of 'created_at' in PHP?

Answer

If you need to change the value in the reviews array, try this

foreach($reviews as $key => $r) {
   $date = explode(" ", $r['created_at']);
   $date = $date[0];
   $time = date_format(date_create($date), "D F d, Y");
   $reviews[$key]['created_at'] = $time;
}