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

PhpArraysAssociative Array

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

This will work too!

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

Yay for alternatives!

Solution 3 - Php

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"]); 
});

Solution 4 - Php

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); 
    } 
});

Solution 5 - Php

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 "&".

Solution 6 - Php

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

Solution 7 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPHPLoverView Question on Stackoverflow
Solution 1 - PhpRikeshView Answer on Stackoverflow
Solution 2 - PhpMulanView Answer on Stackoverflow
Solution 3 - PhpSJ00View Answer on Stackoverflow
Solution 4 - PhpNaveenkumar NView Answer on Stackoverflow
Solution 5 - PhpmedinaView Answer on Stackoverflow
Solution 6 - PhpIvan FretesView Answer on Stackoverflow
Solution 7 - PhpCoBoltView Answer on Stackoverflow