0

I have a string like below

Final-Recipient: rfc822;[email protected]
Action: failed
Status: 5.1.1
Diagnostic-Code: smtp;550 5.1.1 RESOLVER.ADR.RecipNotFound; not found

I need to get each value as an associative array like

array('Final-Recipient'=>'rfc822;[email protected]',
      'Action'=>'failed',
      'Status'=>'5.1.1',....)

I was try with explode function,But it don't give result expected.

    $result = array();
    $temp =  explode(';',$message);
    foreach($temp as $value)
    {
     $temp2   = explode(':',$value);
     $result[$temp[0]] = $result[$temp[1]];     
    }
    print_r($result);
3
  • 1
    Show what you've tried. Otherwise we assume you didn't and just want us to do your work for you. Commented Mar 30, 2015 at 13:35
  • @JohnConde I will post soon,Please wait Commented Mar 30, 2015 at 13:38
  • 3
    Hint: why are you exploding by ; when it's obviously not the delimiter in your input data? Commented Mar 30, 2015 at 13:43

1 Answer 1

4
<?php
    $str = 'Final-Recipient: rfc822;[email protected]
    Action: failed
    Status: 5.1.1
    Diagnostic-Code: smtp;550 5.1.1 RESOLVER.ADR.RecipNotFound; not found';
        $res = array ();
        foreach (explode (PHP_EOL, $str) as $e) {
            $t = explode (':', $e);
            $res[trim($t[0])] = trim($t[1]);
        }
        var_dump($res);
Sign up to request clarification or add additional context in comments.

1 Comment

I'd suggest you split the lines by : followed by space instead of just :. Otherwise you should trim $t[1].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.