Search code examples
phpstringdateutc

Add a string to each date in an array


This is a simple questions. I do not know i'm not able to achieve this. How to add a string(Date.UTC) to each date in the row using php like show below

[Date.UTC(2011,11,20),5],[Date.UTC(2011,11,21),5]

As of now i have my result like this, just need to add Date.UTC string to each date in the row.

[["2011,09,03","1"],["2011,09,06","53"]] 

Solution

  • The data you have seems to be a valid json string so...

    $json = '[["2011,09,03","1"],["2011,09,06","53"]]' ;
    $dates = json_decode($json, true);
    
    var_dump($dates);
    
    foreach($dates as &$date){
        $date[0] = "Date.UTC({$date[0]})" ;
    }
    
    var_dump($dates);
    //Pack back to json if you need $json = json_encode($dates);