Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to explode my array to look like this:

[0] => Array
  (
      [0] => 14 // this is hours
      [1] => 38 // this is minutes
      [2] => 14 // this is hours again
      [3] => 59 // this is minutes again
  )
[1] => Array
  (
      [0] => 15 // this is hours
      [1] => 10 // this is minutes
      [2] => 16 // this is hours again
      [3] => 40 // this is minutes again
  )
  .
  .
  .
 [200] => Array
  (
      [0] => 13 // this is hours
      [1] => 35 // this is minutes
      [2] => 23 // this is hours again
      [3] => 32 // this is minutes again
  )

This because i will compare these times in the future.

I have list of times like this:

15:48,16:10 12:01,12:19 13:06,13:28 10:45,11:02

And now i got it in a array which looks like this:

[0] => 16:10,16:36
[1] => 13:06,13:17
.
.
.
[200] => 14:38,14:59

What i have tried so far

   $length = count($timesArr);
    for($i=0; $i < $length; $i++){
        foreach (explode(',', $timesArr[$i]) as $piece) {


            $timesArray[] = explode(':', $piece);
        }
     }  

and this is quite close because the result output is like this:

   [0] => Array
    (
       [0] => 14
       [1] => 38
    )

   [1] => Array
    (
       [0] => 14
       [1] => 59
    )

so again the main problem is that i need as seen above the cells 0 and 1 to be in the same cell

I can also go for it straight from string so i would be exploding strings then.

share|improve this question
    
You could use a regex with preg_replace_callback() to process it –  Martijn Feb 27 '14 at 20:02

5 Answers 5

up vote 0 down vote accepted

Here is another solution

$times = array( "16:10,16:36", '13:06,13:17');
$startendtimes = array();
foreach($times as $time){
    list($start,$end) = explode(',',$time);
    list($starthour,$startmin) = explode(':',$start);
    list($endhour,$endmin) = explode(':',$end);
    $startendtimes[] = array( $starthour,$startmin,$endhour,$endmin );

}
var_dump($startendtimes);
/* Outputs  
 array (size=2)
  0 => 
    array (size=4)
      0 => string '16' (length=2)
      1 => string '10' (length=2)
      2 => string '16' (length=2)
      3 => string '36' (length=2)
  1 => 
    array (size=4)
      0 => string '13' (length=2)
      1 => string '06' (length=2)
      2 => string '13' (length=2)
      3 => string '17' (length=2)
*/
share|improve this answer

This easy, though preg_split() is cool:

$string = '15:48,16:10 12:01,12:19 13:06,13:28 10:45,11:02';

$array = array_chunk(explode(':', str_replace(array(',',' '), ':', $string)), 4);

print_r($array);
share|improve this answer

preg_split('/[,:]/', $string) should split the time string at both : and ,

share|improve this answer

preg_split() should be sufficient
This works -

$timesArr = Array(
    "16:10,16:36",
    "13:06,13:17",
    "14:38,14:59"
);
foreach($timesArr as $time){
    $res[] = preg_split("/[\,\:]/",$time);
}
var_dump($res);
/*
array
  0 => 
    array
      0 => string '16' (length=2)
      1 => string '10' (length=2)
      2 => string '16' (length=2)
      3 => string '36' (length=2)
  1 => 
    array
      0 => string '13' (length=2)
      1 => string '06' (length=2)
      2 => string '13' (length=2)
      3 => string '17' (length=2)
  2 => 
    array
      0 => string '14' (length=2)
      1 => string '38' (length=2)
      2 => string '14' (length=2)
      3 => string '59' (length=2)
*/
share|improve this answer

You almost have it. The simplest solution from your current position would be to use array_merge.

http://uk1.php.net/array_merge

If you pass in the first two (0 and 1) it will combine them into a single array.

share|improve this answer
    
Another possibility is to change how the string is stored in the first array. If for example you were to replace all colons with commas before storing them in the $times[] then you would only have to do one explode() and no array_merge. This is of course assuming that modifying the values stored in $times[] is acceptable in this particular project. –  nick Feb 27 '14 at 20:09
    
well i guess the array merge is not sufficient for this solution since the amount of these arrays changes depending on user file input. Anyway thanks for rapid answers! :) –  Bloof Feb 27 '14 at 20:21
    
What's the problem with it? You can pass in multiple arrays. It doesn't have to just be two. –  Nathan Dawson Feb 27 '14 at 20:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.