0

Hello I am looping an array like:

Array
(
[01/04/2013 - 14:54] => Array
        (
            [0] => 13528
            [1] => status
            [2] => Waiting
            [3] => 10000
            [4] => Message to user
            [5] => Message3 to my dear user
        )

[01/04/2013 - 14:14] => Array
        (
            [0] => 13523
            [1] => status
            [2] => Waiting
            [3] => 10000
            [4] => Message to user
            [5] => Message2 to my dear user
        )

[12/20/2012 - 09:13] => Array
        (
            [0] => 13476
            [1] => status
            [2] => Waiting
            [3] => 10000
            [4] => Message to user
            [5] => Message do my dear user
       )
);

What I need is to display it properly avoiding duplicates: Here is my code.

foreach ($histories_by_date as $date => $events) {
$output .= $date;
foreach ($events as $key => $event) {
    switch ($events[$key]) {
        case 'Message from user':
            $output .= $events[$key + 1];
            break;
        case 'Message to user':
            $output .= $events[$key + 1];
            break;
       }
    }//foreach2
    }//foreach1

What is annoying is I get duplicates like:

01/04/2013 - 14:54 (CET)
Message3 to my dear user

01/04/2013 - 14:14 (CET)
Message3 to my dear user
Message2 to my dear user

12/20/2012 - 09:13 (CET)
Message3 to my dear user
Message2 to my dear user
Message do my dear user

I only need to display as in original array.

2
  • for me the code works fine and it outputs the original array in the desired format. Commented Jan 7, 2013 at 10:33
  • instead of var_dump, can you show your array. Commented Jan 7, 2013 at 10:38

1 Answer 1

2

Should work like this:

$tmp = '';
$output = '';
foreach ($histories_by_date as $date => $events) {
    $output .= $date;
    foreach ($events as $key => $event) {
      switch ($events[$key]) {
        case 'Message from user':
            $tmp  .= $events[$key + 1];
            break;
        case 'Message to user':
            $tmp .= $events[$key + 1];
            break;
       }
    }//foreach2
    $output.=$tmp;
    $tmp = '';
}//foreach1

Alternative solution would be to break the foreach loop to, not only the switch like this:

$output = "";
foreach ($histories_by_date as $date => $events) {
    $output .= $date;
    foreach ($events as $key => $event) {
        $tmp = '';
        switch ($events[$key]) {
            case 'Message from user':
                $tmp  .= $events[$key + 1];
                break 2;
            case 'Message to user':
                $tmp .= $events[$key + 1];
                break 2;
           }
    }//foreach2

    $output.=$tmp;
}//foreach1
echo $output;
Sign up to request clarification or add additional context in comments.

5 Comments

For some reason my $tmp is constantly erased before displayed.
It seems it only works when is placed before }//foreach2 otherwise the variable is not visible. Can you confirm that?
@Kandinski Yes, my fault, I've updated the code. The tmp was set to empty string for each single event, this was wrong, you have to reset it after the loop.
Thanks a lot, I am using the second version.
I guess the first version is working also by displaying $tmp outside both foreach.

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.