64

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.

How can this issue be resolved?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
PHPLover
  • 1
  • 51
  • 158
  • 311

7 Answers7

118

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.

Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • 7
    Updating the foreach variable array within its scope will not alter its value. http://stackoverflow.com/questions/5810168/php-foreach-by-reference-causes-weird-glitch-when-going-through-array-of-objects – Raj Mar 18 '13 at 08:15
46

This will work too!

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

Yay for alternatives!

Mulan
  • 129,518
  • 31
  • 228
  • 259
  • 4
    this is the correct answer .. you need a & - the other way (accepted) is sort of a subtle "hack" - i.e the `accepted answer` by @Rikesh works `indirectly` for reasons that are not obvious, i.e. to many it might look like a) works, but actually b) is happening, when you needed c) all along.. @naomik's answer is the right one "c)" and while "a)" will work it is not the way to go – Mr Heelis Oct 22 '15 at 11:15
21

PHP array_walk() function is specifically for altering array.

Try this:

array_walk ( $data, function (&$key) { 
    $key["transaction_date"] = date('d/m/Y',$key["transaction_date"]); 
});
Abhishek Pakhare
  • 476
  • 1
  • 4
  • 15
SJ00
  • 638
  • 1
  • 6
  • 10
  • Thanks! 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. – yenren Apr 26 '19 at 06:30
9

Use array_walk_recursive function for multi-denominational array.

array_walk_recursive($data, function (&$v, $k) { 
    if($k == 'transaction_date'){ 
        $v = date('d/m/Y',$v); 
    } 
});
Naveenkumar N
  • 101
  • 1
  • 2
2

The best approach is using a lambda within "array_walk" native function to make the required changes:

    array_walk($data, function (&$value, $key) {
            if($key == 'transaction_date'){ 
                  $value = date('d/m/Y', $value); 
            }
    });

The value is updated in the array as it's passed with as a reference "&".

medina
  • 8,051
  • 4
  • 25
  • 24
1

This a single solution, in where your_field is a field that will set and new_value is a new value field, that can a function or a single value

foreach ($array as $key => $item) {
   $item["your_field"] = "new_value";
   $array[$key] = $item;
}

In your case new_value will be a date() function

Ivan Fretes
  • 668
  • 7
  • 11
0
foreach($data as $value)
{
    $value["transaction_date"] = date('d/m/Y',$value["transaction_date"]);
}
return $data;
CoBolt
  • 442
  • 2
  • 11