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.

link|improve this question

17% accept rate
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 at 8:19
feedback

3 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.

link|improve this answer
I've updated my code. – scessor Jan 27 at 8:36
feedback

This will do.

$m = preg_split('/WINDMILL_\d+/is', $data, -1, PREG_SPLIT_NO_EMPTY);
$r = array();
$len = count($m);

for($i=0;$i<$len; $i++){
    $d = $m[$i];
    $s = explode(',', $d);
    $s = array_filter($s);
    $r[$i] = array();
    foreach($s as $t){
        $ex = preg_split('/\s+/', trim($t), 2);
        $k = $ex[0];
        $v = isset($ex[1])? $ex[1]: '';
        $r[$i][$k] = $v;
    }
}
print_r($r);

output

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

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

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

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

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

[5] => Array
    (
        [DOOR] => OPEN
        [ABSWITCH1] => OPEN
        [ABSENSE1] => OPEN
        [ABSWITCH2] => OPEN
        [ABSENSE2] => OPEN
        [STATUS] =>
        [ALARM] => ON Time:12:46:01 : 25/01/2012
    )
)
link|improve this answer
feedback

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
        )

)
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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