dadazhang
V2EX  ›  PHP

怎么吧一个字符串冒号前面的值当做数组的键,把冒号后面的值当做数组的值

  •  1
     
  •   dadazhang · Apr 11, 2017 · 4046 views
    This topic created in 3493 days ago, the information mentioned may be changed or developed.

    Domain Name: dada.com,
    Registry Domain ID: 102993_DOMAIN_COM-VRSN,
    Registrar WHOIS Server: whois.pairNIC.com,
    Registrar URL: https://www.pairNIC.com,
    Updated Date: 2014-02-03T19:00:24+0000Z,
    Creation Date: 1995-03-04T05:00:00+0000Z,
    Registrar Registration Expiration Date: 2018-03-05T05:00:00+0000Z,
    Registrar: pairNIC INC,
    Registrar IANA ID: 99,
    Registrar Abuse Contact Email: [email protected],
    Registrar Abuse Contact Phone: +1.8887247642,
    Domain Status: clientDeleteProhibited,
    Domain Status: clientTransferProhibited,
    Domain Status: clientUpdateProhibited,
    Registry Registrant ID: ,
    Registrant Name: Juan-Carlos Sobrino,
    Registrant Organization: ,
    Registrant Street: 47 Saint Mark's Place ,
    Registrant City: New York,
    Registrant State/Province: NY,
    Registrant Postal Code: 10003,
    Registrant Country: US,
    Registrant Phone: +1.6465551234,
    Registrant Phone Ext:,

    就类似这样的数据,有的冒号后面还是空的值。。。怎么解决。。。。求助

    10 replies  •  2017-04-19 12:53:59 +08:00
    silentoy
    silentoy
    Apr 11, 2017❤️1
    1
    ```php
    $data = explode("\n", $str);
    $res = array();

    foreach ($data as $item) {
    list($key, $value) = explode(":", $item);
    $res[trim($key)] = trim($value, ' ,');
    }

    var_dump($res);
    ```
    kghch
    kghch
    Apr 11, 2017
    2
    lines = data.split(',')
    res = {}
    for line in lines:
    line= line.split(':')
    k = line[0]
    v = ''
    if len(line) > 1:
    v = line[1]
    res[k] = v

    print res
    dadazhang
    dadazhang
    OP
    Apr 11, 2017
    3
    @silentoy 谢谢,完美解决
    kghch
    kghch
    Apr 11, 2017
    4
    #2 ,格式丢了,不好意思。
    nfroot
    nfroot
    Apr 11, 2017
    5
    php 的数组 key 还能用空格啊
    wangjie
    wangjie
    Apr 11, 2017
    6
    let a={}
    data.split(',')
    .map((t)=>{
    let l=t.split(':')
    a[l[0]]=l[1]
    })
    console.log(a)
    Tyanboot
    Tyanboot
    Apr 11, 2017 via Android
    7
    …楼上用 Python 和 JS 回答的是在想啥。
    jea
    jea
    Apr 18, 2017
    8
    @Tyanboot 论语言的多样性
    Balthild
    Balthild
    Apr 19, 2017 via Android
    9
    @jea 这里是 /go/php
    jea
    jea
    Apr 19, 2017
    10
    @Balthild 我的意思是他们[手动滑稽]
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2993 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 00:40 · PVG 08:40 · LAX 16:40 · JFK 19:40
    ♥ Do have faith in what you're doing.