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

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入