0

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.

1
  • 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... Commented Jan 27, 2012 at 8:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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
        )

)

Comments

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.