Shijin TR
Shijin TR

Reputation: 7768

Create array from string using php

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);

Upvotes: 0

Views: 84

Answers (1)

Halayem Anis
Halayem Anis

Reputation: 7785

<?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);

Upvotes: 4

Related Questions