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.

Here is my Input:

WINDMILL_1 DOOR OPEN , ABSWITCH1 OPEN , ABSENSE1 OPEN , ABSWITCH2 OPEN , ABSENSE2 OPEN , EVENT Time:11:30:00 : 03/08/2096 WINDMILL_1 DOOR OPEN , ABSWITCH1 OPEN , ABSENSE1 OPEN , ABSWITCH2 OPEN , ABSENSE2 OPEN , EVENT Time:11:30:00 : 03/08/2096 WINDMILL_1 DOOR OPEN , ABSWITCH1 OPEN , ABSENSE1 OPEN , ABSWITCH2 OPEN , ABSENSE2 OPEN , STATUS , ALARM ON Time:12:46:01 : 25/01/2012

MY OUTPUT

Array(
    [0] => Array(
        [0] => DOOR OPEN
        [1] => ABSWITCH1 OPEN
        [2] => ABSENSE1 OPEN
        [3] => ABSWITCH2 OPEN
        [4] => ABSENSE2 OPEN
        [5] => EVENT Time:11:30:00 : 03 / 08 / 2096
    )

    [1] => Array(
        [0] => DOOR OPEN
        [1] => ABSWITCH1 OPEN
        [2] => ABSENSE1 OPEN
        [3] => ABSWITCH2 OPEN
        [4] => ABSENSE2 OPEN
        [5] => EVENT Time:11:30:00 : 03 / 08 / 2096
    )

    [2] => Array(
        [0] => DOOR OPEN
        [1] => ABSWITCH1 OPEN
        [2] => ABSENSE1 OPEN
        [3] => ABSWITCH2 OPEN
        [4] => ABSENSE2 OPEN
        [5] => STATUS
        [6] => ALARM ON Time:12:46:01 : 25 / 01 / 2012
    )
)

I managed to bring the above output with this code

$arr = explode("|", $string);
foreach ($arr as $key => $val)
{
    $arr[$key] = explode(',', $val);
}         
print_r($arr);

But what i need is

Array (
    [0] => Array (
        [DOOR] => OPEN
        [ABSWITCH1] => OPEN
        [ABSENSE1] => OPEN
        [ABSWITCH2] => OPEN
        [ABSENSE2] => OPEN
        [EVENT] => Time:11:30:00 : 03/08/2096
    ),    
    ...
)

Please help me out.

share|improve this question
    
I think another nested foreach loop where you explode by space would do the trick, that would only give a problem with the event times as they would be split as well... –  Joep Jan 27 '12 at 8:19
add comment

2 Answers

Change to:

$tmp1 = explode('|', $string);
foreach ($tmp1 as $key1 => $val1) {
    $tmp2 = explode(",", $val1);
    foreach ($tmp2 as $key2 => $val2) { 
        $tmp3 = explode(' ', trim($val2));
        $key = ( $tmp3[count($tmp3) - 2] == ':' ? 'EVENT' : $tmp3[count($tmp3) - 2] );
        $newArr[$key1][$key] = $tmp3[count($tmp3) - 1];
    }
}
print_r($newArr);

Also see this example.

share|improve this answer
    
I've updated my code. –  scessor Jan 27 '12 at 8:36
add comment

I'm a bit confused as don't see any pipe delimiter in your string. Anyway you can use this code:

// $str is your original string
$arr = array();
foreach (explode('WINDMILL_1 ', $str) as $s) {
    if (trim($s) != "")
       $arr[] = explode(', ', $s);
}
print_r($arr);

OUTPUT:

Array
(
    [0] => Array
        (
            [0] => DOOR OPEN 
            [1] => ABSWITCH1 OPEN 
            [2] => ABSENSE1 OPEN 
            [3] => ABSWITCH2 OPEN 
            [4] => ABSENSE2 OPEN 
            [5] => EVENT Time:11:30:00 : 03/08/2096 
        )

    [1] => Array
        (
            [0] => DOOR OPEN 
            [1] => ABSWITCH1 OPEN 
            [2] => ABSENSE1 OPEN 
            [3] => ABSWITCH2 OPEN 
            [4] => ABSENSE2 OPEN 
            [5] => EVENT Time:11:30:00 : 03/08/2096 
        )

    [2] => Array
        (
            [0] => DOOR OPEN 
            [1] => ABSWITCH1 OPEN 
            [2] => ABSENSE1 OPEN 
            [3] => ABSWITCH2 OPEN 
            [4] => ABSENSE2 OPEN 
            [5] => STATUS 
            [6] => ALARM ON Time:12:46:01 : 25/01/2012
        )

)
share|improve this answer
add comment

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.