Jump to content

[SOLVED] Converting a 3rd party array string to PHP Array


JasonO

Recommended Posts

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

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

?>

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! :D

 

Thanks for your help, never thought it would be that easy  ;D

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/

<?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 :(

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.