-4

I was wondering is it possible to convert the following array:

Array (
    "2016-03-03 19:17:59",
    "2016-03-03 19:20:54",
    "2016-05-03 19:12:37"
)

Into this:

Array (
    "2016-03-03",
    "2016-03-03",
    "2016-05-03"
)

Without creating any loops?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Beqa
  • 101
  • 2
  • 14
  • 5
    Gonna ask the obvious question. What's wrong with loops....? – Jonnix Oct 09 '16 at 21:56
  • 4
    *"Without creating any loops?"* **NO.** At the end you need to change each array element so you have to loop over the array. Just search how to convert one date format into another and how to apply a function to each array element. – Rizier123 Oct 09 '16 at 21:56
  • 2
    an odd number of questions ask: how to do X without (insert basic php\programming term\function here) –  Oct 09 '16 at 22:01
  • @nogad there is a simple explanation: they have read somewhere that "loops are slow in php". – zerkms Oct 09 '16 at 22:01
  • ok, but is is basically meaningless. –  Oct 09 '16 at 22:03
  • 1
    There's even a simpler explanation (Occam's Razor anyone?)... the homework says "do not use loops" :D – pid Oct 09 '16 at 22:03
  • why set homework that has no basis in reality? oh right i remember school now :-) –  Oct 09 '16 at 22:04
  • i expect the OP not to care but benchmarking of 10 million iterations gives an average of 23 seconds for foreach() vs array_map of 30 seconds –  Oct 09 '16 at 22:21
  • http://ideone.com/KzmfKd =23 seconds. http://ideone.com/RVGgTm =30 seconds –  Oct 09 '16 at 22:27

3 Answers3

9

There's no explicit loops, if you can use array_map, although internally it loops:

function format_date($val) {
  $v = explode(" ", $val);
  return $v[0];
}
$arr = array_map("format_date", $arr);

From the PHP Manual:

array_map() returns an array containing all the elements of array1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map().

Also, when you are dealing with Dates, the right way to do is as follows:

return date("Y-m-d", strtotime($val));

The simple way, using loops is to use a foreach():

foreach($arr as $key => $date)
  $arr[$key] = date("Y-m-d", strtotime($date));

This is the most simplest looping way I can think of considering the index to be anything.


Input:

<?php
$arr = array(
    "2016-03-03 19:17:59",
    "2016-03-03 19:20:54",
    "2016-05-03 19:12:37"
);
function format_date($val) {
  $v = explode(" ", $val);
  return $v[0];
}
$arr = array_map("format_date", $arr);

print_r($arr);

Output

Array
(
    [0] => 2016-03-03
    [1] => 2016-03-03
    [2] => 2016-05-03
)

Demo: http://ideone.com/r9AyYV

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
2

Yes, use map:

function first10($s) {
    return substr($s, 0, 10);
}

$result = array_map("first10", $yourArray);

WARNING: this is a good solution only if you are sure that the date format does not change, in other words the first 10 characters must contain the date.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
pid
  • 11,472
  • 6
  • 34
  • 63
1

Praveen Kumar's answer is probably the better solution but there is a way to do with what wouldn't really been seen as a loop. instead you use recursion

function explodeNoLoop($array,$delim,$index=0)
{
    $returnArr = array();
    if(isset($array[$index]))
    {
        $expldoed = explode($delim,$array[$index]);
        array_push($returnArr,$expldoed[0]);
    }
    if(isset($array[$index+1]))
    {
        $returnArr = array_merge($returnArr,explodeNoLoop($array,$delim,$index+1));
    }
    return $returnArr;
}

$myArr = array (
    "2016-03-03 19:17:59",
    "2016-03-03 19:20:54",
    "2016-05-03 19:12:37"
);

var_dump(explodeNoLoop($myArr," "));

example

How this code works is that with the function we explode the array at the index provided by the function parameter and add this to our returning array. Then we check if there is a value set at the next index which is +1 of the index we passed into the function. If it exists then we call the function again with the new index with the same array and delimiter. We then merge the results of this with our returner array and then return that.

However, with this one should be careful of nest level errors where you excursively call a function too many times, like looking into the reflection of a mirror in a mirror.

Community
  • 1
  • 1
Memor-X
  • 2,870
  • 6
  • 33
  • 57