Josh Jogns Josh Jogns - 1 month ago 5
PHP Question

Converting String to Array with php

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

Answer

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