dongxingguo1978 2016-10-22 09:44
浏览 88
已采纳

使用php将String转换为数组

Recently I have been having an issue with converting a string to an array. My method might not be the best or I'm just missing something. Any help would be nice.

My array (this cannot be changed):

$lic = "[[`license_civ_driver`,1],[`license_civ_boat`,0],[`license_civ_pilot`,0],[`license_civ_trucking`,0],[`license_civ_gun`,1],[`license_civ_dive`,0],[`license_civ_home`,0],[`license_civ_oil`,0],[`license_civ_diamond`,0],[`license_civ_salt`,0],[`license_civ_sand`,0],[`license_civ_iron`,0],[`license_civ_copper`,0],[`license_civ_cement`,0],[`license_civ_medmarijuana`,0],[`license_civ_cocaine`,0],[`license_civ_heroin`,0],[`license_civ_marijuana`,0],[`license_civ_rebel`,0]]"; 

The result I need:

'license_civ_driver' => '1', 'license_civ_boat' => '0', 'license_civ_pilot' => '0', 'license_civ_trucking' => '0', 'license_civ_gun' => '1', 'license_civ_dive' => '0', 'license_civ_home' => '0', 'license_civ_oil' => '0', 'license_civ_diamond' => '0', 'license_civ_salt' => '0', 'license_civ_sand' => '0', 'license_civ_iron' => '0', 'license_civ_copper' => '0', 'license_civ_cement' => '0', 'license_civ_medmarijuana' => '0', 'license_civ_cocaine' => '0', 'license_civ_heroin' => '0', 'license_civ_marijuana' => '0', 'license_civ_rebel' => '0'

Here is what I'm currently doing

$lic = str_replace('[', "", $lic);
$lic = str_replace(']', "", $lic);
$lic = str_replace('"', "", $lic);
$lic = str_replace(',1', " => '1'", $lic);
$lic = str_replace(',0', " => '0'", $lic);
// $licenses = str_replace('0,', " ", $licenses);
$lic = str_replace(',', ', ', $lic);
$lic = str_replace('`', "'", $lic);

The result:

'license_civ_driver' => '1', 'license_civ_boat' => '0', 'license_civ_pilot' => '0', 'license_civ_trucking' => '0', 'license_civ_gun' => '1', 'license_civ_dive' => '0', 'license_civ_home' => '0', 'license_civ_oil' => '0', 'license_civ_diamond' => '0', 'license_civ_salt' => '0', 'license_civ_sand' => '0', 'license_civ_iron' => '0', 'license_civ_copper' => '0', 'license_civ_cement' => '0', 'license_civ_medmarijuana' => '0', 'license_civ_cocaine' => '0', 'license_civ_heroin' => '0', 'license_civ_marijuana' => '0', 'license_civ_rebel' => '0'

However when converting this to an array the string it is classed as 1 value like this:

$lic = array("'license_civ_driver' => '1', 'license_civ_boat' => '0', 'license_civ_pilot' => '0', 'license_civ_trucking' => '0', 'license_civ_gun' => '1', 'license_civ_dive' => '0', 'license_civ_home' => '0', 'license_civ_oil' => '0', 'license_civ_diamond' => '0', 'license_civ_salt' => '0', 'license_civ_sand' => '0', 'license_civ_iron' => '0', 'license_civ_copper' => '0', 'license_civ_cement' => '0', 'license_civ_medmarijuana' => '0', 'license_civ_cocaine' => '0', 'license_civ_heroin' => '0', 'license_civ_marijuana' => '0', 'license_civ_rebel' => '0'");

However, I want it each value to be separated like this:

    $lic = array('license_civ_driver' => '1', 'license_civ_boat' => '0', 'license_civ_pilot' => '0', 'license_civ_trucking' => '0', 'license_civ_gun' => '1', 'license_civ_dive' => '0', 'license_civ_home' => '0', 'license_civ_oil' => '0', 'license_civ_diamond' => '0', 'license_civ_salt' => '0', 'license_civ_sand' => '0', 'license_civ_iron' => '0', 'license_civ_copper' => '0', 'license_civ_cement' => '0', 'license_civ_medmarijuana' => '0', 'license_civ_cocaine' => '0', 'license_civ_heroin' => '0', 'license_civ_marijuana' => '0', 'license_civ_rebel' => '0');

Is anyone able to help me with this issue? (I can provide any more informated you need, this is my first post so sorry if im brief.)

  • 写回答

2条回答 默认 最新

  • dta38159 2016-10-22 09:52
    关注

    try this:

        $string ='license_civ_driver' => '1', 'license_civ_boat' => '0', 'license_civ_pilot' => '0', 'license_civ_trucking' => '0', 'license_civ_gun' => '1', 'license_civ_dive' => '0', 'license_civ_home' => '0', 'license_civ_oil' => '0', 'license_civ_diamond' => '0', 'license_civ_salt' => '0', 'license_civ_sand' => '0', 'license_civ_iron' => '0', 'license_civ_copper' => '0', 'license_civ_cement' => '0', 'license_civ_medmarijuana' => '0', 'license_civ_cocaine' => '0', 'license_civ_heroin' => '0', 'license_civ_marijuana' => '0', 'license_civ_rebel' => '0'
       $arr =  explode(',',$string);
     print_r($arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++对比不同编码下相同字符串
  • ¥15 pyqt5设计界面控件之间的空白想消除
  • ¥15 arcgis中更新地理配准后几个图层分离怎么解决?
  • ¥15 想请教大家关于机器学习的问题
  • ¥15 虚幻UE保存Actor位置失败
  • ¥15 多个.nii合并为.nii.gz格式
  • ¥50 现在需要统计2023年全年 “签收时间(qssj)” 到 “提交时间(tjsj)” 小于半小时的业务量,但是要排除掉周末的时间,同时还要排除中午休息的时间。
  • ¥50 opencv模板匹配为何图像差别大仍然匹配数值高?
  • ¥15 如何成功完成主菜单和分菜单的代码编程C++
  • ¥15 怎样采集或者其它途径拿到全国最新个体工商户数据