JasonO Posted July 30, 2009 Share Posted July 30, 2009 Hi guys, I've got a problem trying to convert a string to a PHP array. I've been thinking of writing a function to do this, but not sure where to start. I thought about exploding the data out, but then I have a problem if there is an array within the array (which is quite common with the data I have). The format of the array is like so: ["test1","1234",500,500,"",4] Some arrays may be like this, with other arrays inside them: ["test1",[1,2,3,4],500,[5,262,"test2"],"",4] Is there any easy way to go about this? Thanks for your help. Regards, Jason Quote Link to comment https://forums.phpfreaks.com/topic/168158-solved-converting-a-3rd-party-array-string-to-php-array/ Share on other sites More sharing options...
Adam Posted July 30, 2009 Share Posted July 30, 2009 Try json_decode. Quote Link to comment https://forums.phpfreaks.com/topic/168158-solved-converting-a-3rd-party-array-string-to-php-array/#findComment-886904 Share on other sites More sharing options...
mattal999 Posted July 30, 2009 Share Posted July 30, 2009 Well I worked this function up. It can only go two arrays deep. <?php $string = "[\"test1\",[1,2,3,4],500,[5,262,\"test2\"],\"\",4]"; $array = array(); $array2 = array(); $inarray2 = false; $string = substr($string, 1); $string = substr($string, 0, -1); $string = explode(",", $string); foreach($string as $value) { if($inarray2 == true) { if(substr($value, -1) == "]") { $array2[] = substr($value, 0, -1); array_push($array, $array2); $array2 = array(); $inarray2 = false; } else { $array2[] = $value; } } else { if(substr($value, 0, 1) == "[") { $array2[] = substr($value, 1); $inarray2 = true; } else { $array[] = $value; } } } print_r($array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/168158-solved-converting-a-3rd-party-array-string-to-php-array/#findComment-886913 Share on other sites More sharing options...
JasonO Posted July 30, 2009 Author Share Posted July 30, 2009 Amazing, theres a PHP function already that can do the trick! Json_decode works brilliantly ["test1","1234",500,500,"",4] With that being put through json_decode.. Array ( [0] => test1 [1] => 1234 [2] => 500 [3] => 500 [4] => [5] => 4 ) Brilliant! Even works with multiple arrays in an array! Thanks for your help, never thought it would be that easy Quote Link to comment https://forums.phpfreaks.com/topic/168158-solved-converting-a-3rd-party-array-string-to-php-array/#findComment-886914 Share on other sites More sharing options...
JasonO Posted July 30, 2009 Author Share Posted July 30, 2009 Update. It works to a point, I found that since it's not actually JSON code there are times where the function will fail. [bank,"TNL_bank_acc_123456"] This fails because bank is meant to be a string, but is not represented as one with speech marks around it. Using this validator I found this to be the cause of the issue: http://www.jsonlint.com/ Quote Link to comment https://forums.phpfreaks.com/topic/168158-solved-converting-a-3rd-party-array-string-to-php-array/#findComment-886926 Share on other sites More sharing options...
Adam Posted July 30, 2009 Share Posted July 30, 2009 You'll be able to solve that problem simply enough with a regular expression. I've finished work now though, off home! I'll be on later on if you still need help. Quote Link to comment https://forums.phpfreaks.com/topic/168158-solved-converting-a-3rd-party-array-string-to-php-array/#findComment-886933 Share on other sites More sharing options...
JasonO Posted July 30, 2009 Author Share Posted July 30, 2009 I'm looking up and seeing things based around the preg_match function, but I am unable to find a correct pattern to detect if it has quotation marks around it or not. Quote Link to comment https://forums.phpfreaks.com/topic/168158-solved-converting-a-3rd-party-array-string-to-php-array/#findComment-886943 Share on other sites More sharing options...
JasonO Posted July 30, 2009 Author Share Posted July 30, 2009 <?php $string1 = "Blah"; $pattern = "/[A-z]/"; if (preg_match($pattern, $string1, $matches)) { echo "Match was found <br />"; echo $matches[0]; } else { echo "No Match"; } ?> I have this, but to check for "quotation marks" around the text, and if theres not add them is way beyond my skill Quote Link to comment https://forums.phpfreaks.com/topic/168158-solved-converting-a-3rd-party-array-string-to-php-array/#findComment-886979 Share on other sites More sharing options...
Adam Posted July 30, 2009 Share Posted July 30, 2009 Hmmm, only given it a quick test... <?php function clean_json($str) { $str = preg_replace('#([\w]+)(,|\])#', "\"$1\"$2", $str); return json_decode($str); } print_r(clean_json('[bank,"TNL_bank_acc_123456"]')); ?> Edit: Made need to alter it slightly for other characters that could be in the data. The draw back is it will add quotes around integers as well, but you could just do a test for that. Quote Link to comment https://forums.phpfreaks.com/topic/168158-solved-converting-a-3rd-party-array-string-to-php-array/#findComment-887019 Share on other sites More sharing options...
JasonO Posted July 31, 2009 Author Share Posted July 31, 2009 Amazing, works a treat. I've managed to sort all the data into tables and it's very neat and easy process thanks to your function. Fantastic, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/168158-solved-converting-a-3rd-party-array-string-to-php-array/#findComment-887291 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.