I have a page where I am creating a PHP array containing the items in a select box and I use json_encode to convert the array into a string that can be inserted into a PostgreSQL database where the field type is TEXT[]. However, when I get the string back and call json_decode on it, I get a blank string.
function encode_array($libraries)
{
$encoded = json_encode($libraries);
// Replace the square brackets with curly braces for PostgreSQL TEXT[] field formatting.
$encoded = str_replace('[', '{', $encoded);
$encoded = str_replace(']', '}', $encoded);
return $encoded;
}
function decode_array($libraries)
{
$decodedStr = str_replace('{', '[', $libraries);
$decodedStr = str_replace('}', ']', $decodedStr);
$decoded = json_decode($decodedStr);
return $decoded;
}
So essentially, I start out with a PHP array, then insert it into the database. So assuming I have an array that looks like:
$libraries = array('LIBRARY_01', 'LIBRARY_02', 'LIBRARY_03');
I should end up with a database entry that looks something like:
'{LIBRARY_01, LIBRARY_02, LIBRARY_03}'
And then once decoded, I should once again end up with a PHP array with the same values as before.
The insertion / encoding seems to work, as I can see the array in the database, and I assume it would complain if the encoded string was malformed.
Does anyone know where I'm going wrong here? For reference, I'm using PostgreSQL 9.3 and PHP 5.6 if it matters.
EDIT: In case there was an confusion, the array is just a simple array of strings, there are no special symbols in any of these strings, just alphanumeric characters. The reason I do a catch-all replace of square brackets to curly braces is because the JSON string output by json_encode will be surrounded in square brackets, but the database requires it to be surrounded in curly braces when I pass it into the TEXT[] field.
-
In the second part, your decode_array() function, you're not just changing the curly brackets that you changed the first time. You're changing ALL the curly brackets back, including the ones that were curly brackets initially. So, now you have none.Adam Winter– Adam Winter2020年05月18日 20:26:40 +00:00Commented May 18, 2020 at 20:26
-
@AdamWinter I don't understand what you mean... there's only a single pair of square brackets enclosing a list of strings. I convert them to curly braces for the database, and when I get it out of the database, I convert the curly braces back to square brackets so it will be in JSON format for the decode function. I'm only using json_encode because it easily converts an array to a comma-separated list enclosed in braces/brackets.Darin Beaudreau– Darin Beaudreau2020年05月18日 22:21:10 +00:00Commented May 18, 2020 at 22:21
-
I'm saying if "$libraries" already has some curly brackets before you do anything with it, then those will get changed into square brackets in your decode_array() function, which is not what they origially were. I can't see your original string.Adam Winter– Adam Winter2020年05月18日 22:30:08 +00:00Commented May 18, 2020 at 22:30
-
Alternatively, you might look at "escaping" the square brackets in the string so that you can just insert the string as it is.Adam Winter– Adam Winter2020年05月18日 22:31:29 +00:00Commented May 18, 2020 at 22:31
-
I just read the additional info you added to the question. I'm providing an answer as an answer in a moment.Adam Winter– Adam Winter2020年05月18日 22:37:06 +00:00Commented May 18, 2020 at 22:37
1 Answer 1
So, the main issue here is that json format uses Key-Value pairs, and the inferred indexes don't seem to work. Secondly, using json_decoded([string], TRUE) with the TRUE flag, gives you an associative array, instead of an object.
Try running all of this for a good look at what you have:
<?php
//$libraries = array('0'=>'LIBRARY_01', '1'=>'LIBRARY_02', '2'=>'LIBRARY_03'); //this will not work
$libraries = array('one'=>'LIBRARY_01', 'two'=>'LIBRARY_02', 'three'=>'LIBRARY_03');
var_dump($libraries);
echo '<br>';
$encoded = json_encode($libraries);
echo $encoded;
echo '<br>';
$encoded = str_replace('[', '{', $encoded);
$encoded = str_replace(']', '}', $encoded);
echo 'String: '.$encoded;
echo '<br>';
echo '<br>';
$stdObj = json_decode($encoded);
echo 'var_dump: ';
var_dump($stdObj);
echo '<br>';
echo 'json_encode: '.json_encode($stdObj);
echo '<br>';
foreach($stdObj as $element){
echo $element;
echo '<br>';
}
echo '<br>';
$array = json_decode($encoded, TRUE);
echo 'var_dump associative: ';
var_dump($array);
echo '<br>';
echo 'array[one]: '.$array[one];
echo '<br>';
echo '<br>';
echo '<br>';
echo 'json_encode: '.json_encode($array);
echo '<br>';
?>
However, if you're just using json_encode() as a way to turn a one-dimensional array into a simple string list of comma-separated values, then you might use this quick and dirty method:
$libraries = array('LIBRARY_01', 'LIBRARY_02', 'LIBRARY_03');
$encoded = json_encode($libraries);
$encoded = str_replace('[', '{', $encoded);
$encoded = str_replace(']', '}', $encoded);
//in and out of database
$decodedStr = str_replace('{"', '', $encoded);
$decodedStr = str_replace('"}', '', $decodedStr);
$delimiter = '","';
$libraries = explode($delimiter, $decodedStr);
var_dump($libraries);
Point is, json_decode() is not the exact mirror reversal of json_encode().
Using implode() and explode() are the better choice for your situation:
$libraries = array('LIBRARY_01', 'LIBRARY_02', 'LIBRARY_03');
$commaSeparatedString = implode(',', $libraries);
$string = '{'.$commaSeparatedString.'}';
//in and out of database with a comma-separated list in curly brackets
$stringFromDatabase = str_replace('{"', '', $stringFromDatabase);
$stringFromDatabase = str_replace('"}', '', $stringFromDatabase);
$array = explode(',', $stringFromDatabase);
9 Comments
Explore related questions
See similar questions with these tags.