-5

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?

10
  • 5
    Gonna ask the obvious question. What's wrong with loops....? Commented Oct 9, 2016 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. Commented Oct 9, 2016 at 21:56
  • 2
    an odd number of questions ask: how to do X without (insert basic php\programming term\function here) Commented Oct 9, 2016 at 22:01
  • @nogad there is a simple explanation: they have read somewhere that "loops are slow in php". Commented Oct 9, 2016 at 22:01
  • 1
    There's even a simpler explanation (Occam's Razor anyone?)... the homework says "do not use loops" :D Commented Oct 9, 2016 at 22:03

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

19 Comments

Hm, a call to explode() with only a single argument?
Although this is still loops internally I would expect, I suppose as long as OP can't see them they won't mind :P
some one benchmark this against a foreach loop and explode
Thanks a lot! This is much better and faster solution than looping through the entire array.
@PraveenKumar I didn't downvote, but your first answer revision was a very poor one. What I think you can improve is: 1) Explain that array_map() also loops through the array 2) Maybe also show how to properly convert one date format into another instead of that "hack" for this specific format. 3) Explain what exactly does array_map().
|
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.

2 Comments

@PraveenKumar validation is not the subject of a question
@PraveenKumar Thanks, I added a warning so he knows where the limits of this solution are.
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.

1 Comment

Woooooaaah!!! Wow! But recursion seems to be an overkill for the current situation. LoL. We are thinking creatively.
0

Yes, you can call substr_replace() without a loop or calling array_map(). Truncate the strings from the 10th position. Demo

$array = [
    "2016-03-03 19:17:59",
    "2016-03-03 19:20:54",
    "2016-05-03 19:12:37"
];

var_export(
    substr_replace($array, '', 10)
);

Output:

array (
  0 => '2016-03-03',
  1 => '2016-03-03',
  2 => '2016-05-03',
)

You can also count forward from the back of the string. Demo

var_export(
    substr_replace($array, '', -9)
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.