Php字符串爆炸成特定的数组

use*_*640 0 php

以下是保存联系信息的String.此字符串是动态的,即有时新字段例如:移动电话号码可能加起来或旧字段说:电话号码可能会删除.

                              <?php $str = 
                                "tel: (123) 123-4567
                                fax : (234) 127-1234
                                email : abc@a.a";
                                $newStr =  explode(':', $str);
                                echo '<pre>'; print_r($newStr); 
                              ?>
Run Code Online (Sandbox Code Playgroud)

输出的代码是:

                        Array
                            (
                                [0] => tel
                                [1] =>  (123) 123-4567
                                                                fax 
                                [2] =>  (234) 127-1234
                                                                email 
                                [3] =>  abc@a.a
                            )
Run Code Online (Sandbox Code Playgroud)

但所需的输出采用以下格式:

                        Array
                            (
                                [tel] => (123) 123-4567
                                [fax] =>  (234) 127-1234            
                                [email] =>  abc@a.a
                            )
Run Code Online (Sandbox Code Playgroud)

我尝试过以某种方式爆炸......但是没有用.请指导.

Tah*_*ksu 6

$txt = 
                            "tel: (123) 123-4567
                            fax : (234) 127-1234
                            email : abc@a.a";
$arr = array();
$lines = explode("\n",$txt);
foreach($lines as $line){
    $keys = explode(":",$line);
    $key = trim($keys[0]);
    $item = trim($keys[1]);
    $arr[$key] = $item;
}
print_r($arr);
Run Code Online (Sandbox Code Playgroud)

CodePade