dqt20140129 2014-11-27 21:43
浏览 113
已采纳

将字符串转换为key => value对

I have the followed string:

Host                          dnsmgr   Username       Refresh State                Reg.Time
sip.voicetrading.com:5060     N        travelAsk        105 Registered           Thu, 27 Nov 2014 15:57:43
sip.alonia.es:5060            N        user431          225 Registered           Thu, 27 Nov 2014 23:28:24

I want to somehow make this string as associative array.

Something like this:

array(
   'sip.voicetrading.com' => array (
       'port'   => 5060,
       'username' =>'travelAsk',
       'refresh'   => 105,
       'state'     => 'Registered',
       'time'      => 'Thu, 27 Nov 2014 15:57:43'
   ),
   'sip.alonia.es' => array (
       'port'   => 5060,
       'username' =>' user431',
       'refresh'   => 225,
       'state'     => 'Registered',
       'time'      => 'Thu, 27 Nov 2014 23:28:24'
   )
);

Can you suggest me a way how it would be the best way to do it.

So far I tried something like this:

$peer = array(
  "data"=>
"Host                                    dnsmgr Username       Refresh State                Reg.Time                 
sip.voicetrading.com:5060               N      travelIhome        105 Registered           Thu, 27 Nov 2014 16:39:45 " ;


foreach (explode("
", $peer['data']) as $line) {

    $a = strpos('z' . $line, ':') - 1;
    if ($a >= 0) {
        $arrTrunkSettings = explode (' ', trim(substr($line, $a + 1)));
        foreach ($arrTrunkSettings as $key => $value) {
            if (!empty($value)) {
                $data[trim(substr($line, 0, $a))][] = $value;   
            }
        }

    }
}

and the Output is like this array with numeric indexes: But this actually is not what I need.

array (
  "sip.voicetrading.com" =>
  array(
    [0]=> "5060"
    [1]=> "N"
    [2]=> "travelAsk"
    [3]=> "105"
    [4]=> "Registered"
    [5]=> "Thu,"
    [6]=> "27"
    [7]=> "Nov"
    [8]=> "2014"
    [9]=> "16:32:45"
  )
)
  • 写回答

5条回答 默认 最新

  • duanjia2415 2014-11-27 23:01
    关注

    EDIT: Shorted the solution quite a bit. This does the same as my first answer, but now in half the code. First it gets all lines, then pulls and splits the headers into a seperate array. Then it can loop the rest of the lines and add them into the result using the headers as keys.

    $result = array();
    
    $lines = explode("
    ", $string);
    $headers = preg_split("/\s+/", array_shift($lines), 6);
    
    foreach ($lines as $line) {
        $part = preg_split('/\s+/', $line, 6);
        $info = explode(":", $part[0]);
        $result[$info[0]]['port'] = $info[1];
        for($i = 1; $i < count($headers); $i++) {
            $result[$info[0]][$headers[$i]] = $part[$i];
        }    
    }
    

    This gives you a result array matching what you asked for. It's gets the keys/value pairs from the headers, so if the input changes, so does the resulting array, unlike the hardcoded solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算