0

Tech used: PHP 5.3.10

Hi, I have an array (example below) I need to manipulate by combining the same times, concat any applicable offerId's to a comma delimted string and finally return the string sorted by time! Not much then :)

I looked at this answer to ID the rows: https://stackoverflow.com/a/1019534/260023 however I drew a blank once I had identified the row how to modily the data in it...

Array
(
[0] => Array
    (
        [dateTime] => 2012-04-03T18:00:00
        [offerIDs] => 
    )

[1] => Array
    (
        [dateTime] => 2012-04-03T18:30:00
        [offerIDs] => 
    )

[2] => Array
    (
        [dateTime] => 2012-04-03T19:00:00
        [offerIDs] => 
    )

[3] => Array
    (
        [dateTime] => 2012-04-03T21:00:00
        [offerIDs] => 
    )


[4] => Array
    (
        [dateTime] => 2012-04-03T18:00:00
        [offerIDs] => 17302
    )

[5] => Array
    (
        [dateTime] => 2012-04-03T18:30:00
        [offerIDs] => 17302
    )

[6] => Array
    (
        [dateTime] => 2012-04-03T19:00:00
        [offerIDs] => 17302
    )

[7] => Array
    (
        [dateTime] => 2012-04-03T19:30:00
        [offerIDs] => 17302
    )

[8] => Array
    (
        [dateTime] => 2012-04-03T20:00:00
        [offerIDs] => 17302
    )

[9] => Array
    (
        [dateTime] => 2012-04-03T20:30:00
        [offerIDs] => 17302
    )

[10] => Array
    (
        [dateTime] => 2012-04-03T19:00:00
        [offerIDs] => 17298
    )

)

This should result in:

Array
(
[0] => Array
    (
        [dateTime] => 2012-04-03T18:00:00
        [offerIDs] => 17302
    )

[1] => Array
    (
        [dateTime] => 2012-04-03T18:30:00
        [offerIDs] => 17302
    )

[2] => Array
    (
        [dateTime] => 2012-04-03T19:00:00
        [offerIDs] => 17302,17298
    )


[6] => Array
    (
        [dateTime] => 2012-04-03T19:30:00
        [offerIDs] => 17302
    )

[7] => Array
    (
        [dateTime] => 2012-04-03T20:00:00
        [offerIDs] => 17302


[8] => Array
    (
        [dateTime] => 2012-04-03T20:30:00
        [offerIDs] => 17302
    )

[9] => Array
    (
        [dateTime] => 2012-04-03T21:00:00
        [offerIDs] => 
    )
)
2
  • Thanks for all the answers so far, looks like standard advice is to convert key to the dateTime so will do that and report back Commented Apr 3, 2012 at 14:15
  • Many thanks everyone, I ticked the answer that I ended up using but pretty much most of you cracked it once you put me on the course of using the datetime as the array key. Thanks again, it was driving me mad! Commented Apr 4, 2012 at 16:11

5 Answers 5

1

Something like this should work:

$newarray = array();

foreach ($array as $elements) {
   if (empty($newarray[$elements['dateTime'])) {
     $newarray[$elements['dateTime']] = $elements['offerIDs'];
   } else {
     $newarray[$elements['dateTime']] .= $elements['offerIDs'];
   }
}

Now run a simple sort by time using the code provided by mingos.

The re-arrange the array:

$finalarray = array();

foreach ($array as $d => $v) {
    $finalarray[] = array('dateTime' = $d, 'offerIDs' => $v);
}
Sign up to request clarification or add additional context in comments.

Comments

1

That's it. convert date to UNIX (for insurance). and create a new array with key of a date

foreach($out as $v){
    $new_out[strtotime($v['dateTime'])] = $v;
}

sort($new_out);

Comments

1

Assume $data is your arrays

# indexed result by time
$tmp = array();
foreach($data as $d) {
    $tmp[$d['dateTime']][] = $d['offerIDs'];
}

# sort keys
ksort($tmp);

# This will result in (then you might have to use implode(',', $offer_ids)
# array(
#    '2012-04-03T19:00:00' => array(1234, 45234),
# ),

# this is what's you expected
$finals = array();
foreach($tmp as $key => $vals) {
    $finals[] = array(
        'dateTime' => $key,
        'offerIDs' => implode(',', $vals),
    ),
}

Comments

1

Have a look at usort() function. It lets you define any custom sorting you need.

The easiest way to sort dates is probably by converting them to timestamps:

$date1 = new Date('2012-04-03T18:00:00');
$date2 = new Date('2012-04-03T20:00:00');
if ($date1->getTimestamp() > $date2->getTimestamp()) {
    // do something
} else {
    // do something else
}

I think these two pieces of info should enable you to write an appropriate sorting function ;).

[EDIT]

Another possibility I've just thought of, after re-reading your post.

You might foreach over all the results and create the output array like this:

foreach ($input as $item) {
    if (!array_key_exists($item['dateTime']),$output) {
        $output[$item['dateTime']] = $item['offerIDs'];
    }
    if (empty($output[$item['dateTime']])) {
        $output[$item['dateTime']] = $item['offerIDs'];
    }
}

So, the output array would become dateTime => offerIDs pairs. You can sort that easily and then re-convert into your intended output array.

Comments

0

First, filter out the array with a regular foreach and then use usort() to sort it out.

The usort function gives you the ability to decide how the sorting should be done (by passing your own comparison function).

2 Comments

So I can loop through the array with foreach, that's fine. However once I have found a matching dateTime, how do I modify this match to add the offerId to it?
After re-reading your objective, I'd say I would change the way you're doing to have the time as the index of the array. Then you simply check if the index is already in use.

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.