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

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算