dongyilu3143 2016-11-24 12:26
浏览 52
已采纳

如何将包含数组值的字符串转换为PHP数组?

I have a string that contains an array like:

$tarray = Array ( [gt_ref_id] => 36493115 [tender_notice_type] => [organisation_name] => ROSALES WATER DISTRICT [address] => No. 5 Bonifacio Street [address2] => [contact_person] => [tender_notice_no] => ROSALWD 2016-011-0144(Ref: 4206661)
)

but I want to access the value of this array like:

echo "ref id = ".$tarray['gt_ref_id'];

It should output like:

ref id = 36493115

but I can't because it's a string. I could if it was an array.

How can I convert the string to a proper PHP array?

  • 写回答

2条回答 默认 最新

  • doujiao2000 2016-11-24 13:58
    关注

    If you want store a php array on you database you can try with json_encode or serialize, and when you would need retrive data from you database, you can use json_decode or unserialize.

    Using JSON

    Examples(store as json):

    $myArray = array('name' => 'Jack', 'lastname' => 'Smith', 24);
    $toStore = json_encode($myArray); // return a string: {"name":"Jack","lastname":"Smith","0":24}
    
    mysqli_query($link,
        'INSERT INTO myTable (data)
        VALUES ("' . mysqli_real_escape_string($link, $toStore) . '")'
    );
    

    Examples(retrieved from json):

    $query = mysqli_query($link, 'SELECT FROM MyTable WHERE id = ...');
    $row = mysqli_fetch_assoc($query);
    $row = json_decode($row); // return a array: array('name' => 'Jack', 'lastname' => 'Smith', 24)
    

    Using serialize

    Example(store serialized): $myArray = array('name' => 'Jack', 'lastname' => 'Smith', 24); $toStore = serialize($myArray); // return a string: a:3:{s:4:"name";s:4:"Jack";s:8:"lastname";s:5:"Smith";i:0;i:24;}

    mysqli_query($link,
        'INSERT INTO myTable (data)
        VALUES ("' . mysqli_real_escape_string($link, $toStore) . '")'
    );
    

    Examples(retrieve serialized):

    $query = mysqli_query($link, 'SELECT FROM MyTable WHERE id = ...');
    $row = mysqli_fetch_assoc($query);
    $row = unserialize($row); // return a array: array('name' => 'Jack', 'lastname' => 'Smith', 24)
    

    After :

    if you need access to you data from converted array, you can call like:

    $row['name'] // return "Jack"
    

    Another ways

    Regexp:

    You can try use regexp, but this not the best way because sometime is too slow to process data, and maybe, if you don't know security aspects, this definitely not is the best way.

    Tokenizer:

    You can try create you own token processor, like template engine, but this is the most large way...

    Learn more about the functions:

    Pros:

    1. Support multiple data-types: multidimentional array, object, integer, float, string, and many more data except resource.
    2. Native support for serialize and unserialize since PHP4.
    3. Native support for json_encode and json_decode since PHP 5.2.0
    4. Fast and secure encode and decode.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装