Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update specific key's value in an associative array in PHP?

I've a following associative array named $data

Array (     [0] => Array         (             [transaction_user_id] => 359691e27b23f8ef3f8e1c50315cd506             [transaction_no] => 19500912050218             [transaction_total_amount] => 589.00             [transaction_date] => 1335932419             [transaction_status] => cancelled         )      [1] => Array         (             [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d             [transaction_no] => 36010512050819             [transaction_total_amount] => 79.00             [transaction_date] => 1336476696             [transaction_status] => cancelled         )      [2] => Array         (             [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d             [transaction_no] => 19020512050820             [transaction_total_amount] => 299.00             [transaction_date] => 1336476739             [transaction_status] => cancelled         )      [3] => Array         (             [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d             [transaction_no] => 27050512050821             [transaction_total_amount] => 79.00             [transaction_date] => 1336476927             [transaction_status] => cancelled         )      [4] => Array         (             [transaction_user_id] => 8e9050a3646c98342b9ba079fba80982             [transaction_no] => 12070512050822             [transaction_total_amount] => 129.00             [transaction_date] => 1336477032             [transaction_status] => cancelled         )  ) 

and I want to convert the value of key [transaction_date] into user readable format (i.e. mm/dd/yyyy). For that I written the following code in a function which returns the whole array:

 foreach($data as $value)  {     $value[transaction_date]=date('d/m/Y',$value[transaction_date]);  }       return $data; 

My problem is I'm getting the same array without changing the value of [transaction_date] for all the array elements. Actually array with updated values for [transaction_date] is expected to be returned. Can anyone help me out to resolve this issue? Thanks in advance.

like image 403
PHPLover Avatar asked Mar 18 '13 07:03

PHPLover


People also ask

How do I change the value of associative array in PHP?

I used this in my own case, to change a certain value in an associative array, by slightly modifying it: array_walk($users, function(&$key) { if ($key['name'] == 'John') $key['name'] = 'Jack'; }); In other words, in users array, change user name to Jack, if it is John.

Can we change the value of an element in an array?

To change the value of all elements in an array:Use the forEach() method to iterate over the array. The method takes a function that gets invoked with the array element, its index and the array itself. Use the index of the current iteration to change the corresponding array element.

What is array_keys () used for in PHP?

The array_keys() function returns an array containing the keys.


2 Answers

Change your foreach to something like this, You are not assigning data back to your return variable $data after performing operation on that.

foreach($data as $key => $value) {   $data[$key]['transaction_date'] = date('d/m/Y',$value['transaction_date']); } 

Codepad DEMO.

like image 188
Rikesh Avatar answered Sep 28 '22 09:09

Rikesh


This will work too!

foreach($data as &$value) {   $value['transaction_date'] = date('d/m/Y', $value['transaction_date']); } 

Yay for alternatives!

like image 43
Mulan Avatar answered Sep 28 '22 08:09

Mulan