Title: Converting one item of each subarray in php
Tags: php;arrays
Question: I have an array of arrays and for each sub array I would like to convert the fourth element from timestamp to datetime. I tried something like this, but I am not sure how to do it in the fastest way possible in case I had so many sub arrays:
$array = array:2 [
0 =&gt; array:4 [
0 =&gt; "x"
1 =&gt; "y"
2 =&gt; "z"
3 =&gt; 1589481768
]
1 =&gt; array:4 [
0 =&gt; "a"
1 =&gt; "b"
2 =&gt; "c"
3 =&gt; 1589481768
];
$result = [];
foreach ($array as $key =&gt; $r) {
$r[3] = date('d/m/Y H:i:s', $r[3]);
array_push($result, $r);
}
I am new in this, so any help is more than welcome!!!
Comment: What exactly is your question? It should work. Do you worry about performance? If so: don't worry (and try not to optimize prematurely).

Comment: @simon.ro, yes it works, I was just wondering how I could find a way to optimize it if I am supposed to work with larger number of sub arrays.

Comment: Do you mean a DateTime like `$r[3] = (new DateTime)->setTimestamp($r[3]);` Or does it already work and you meant to use pass by reference like `foreach ($array as $key => &$r) {` and remove the array_push call.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/15/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><arrays>', 'Title': 'Converting one item of each subarray in php', 'ViewCount': 61, 'id': 61826371, 'user_AboutMe': '', 'user_AccountId': 7602668.0, 'user_DisplayName': 'Felicity', 'user_Reputation': 53.0, 'user_Views': 23.0, 'user_id': 5766658.0}}