How to Convert Array to a String Using Methods Other Than JSON

How to convert array to a string using methods other than JSON?

serialize() is the function you are looking for. It will return a string representation of its input array or object in a PHP-specific internal format. The string may be converted back to its original form with unserialize().

But beware, that not all objects are serializable, or some may be only partially serializable and unable to be completely restored with unserialize().

$array = array(1,2,3,'foo');
echo serialize($array);

// Prints
a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;s:3:"foo";}

Convert array to JSON

Script for backward-compatibility:
https://github.com/douglascrockford/JSON-js/blob/master/json2.js

And call:

var myJsonString = JSON.stringify(yourArray);

Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing. Credit goes to: @Spudley for his comment below

Convert an arrays of array surrounded by quotes into string array

What you've got there is an array serialised to JSON.

You can parse it to an array then map it to the value you want

const array1 = '[["2022-01-18","2022-01-21"],["2022-02-15","2022-02-17"]]';

const arr = JSON.parse(array1);
const array2 = arr.map(([from, to]) => `${from} - ${to}`);
console.log(array2);

JavaScript - Converting string-array to an array of strings

JSON.parse would work, if the JSON wasn't invalid. As mentioned in the comments, the string would have to be formatted as '["1", "2"]'.

If you do not have control over the formatting, you can parse it manually: if the strings don't contain quotes, you can use #replaceAll("'", '"').

If you have edge cases you need to cover, json5 may be able to help: JSON5.parse(str) once you have the script loaded via NPM or unpkg

PHP Change array to string

Implode the array:

$str = "'" . implode("', '", $test) . "'";

Then you can add it to your SQL:

$sql .= "(table.col IN ($str) OR table.col IS NULL)";

Convert array of JSON String to array of object in Python

Now solution looks wired but this works and will be useful for you in optimization. Convert the complete list into str then remove all ' single commas with str function and the apply json loads, hurray this has got worked for me.

data = ['[1,2,3]', '[4,5,6]', '[7,8,9]']
r = str(data).replace("'",'')

import json
data = json.loads(r)

now your data will be of list of list without looping. You can achieve this.

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Reverse of JSON.stringify?

You need to JSON.parse() your valid JSON string.

var str = '{"hello":"world"}';
try {
var obj = JSON.parse(str); // this is how you parse a string into JSON
document.body.innerHTML += obj.hello;
} catch (ex) {
console.error(ex);
}

How to convert array of strings into JSON object?

use Object.fromEntries to construct an object out of your array elements after splitting them using : seperator:

let data = [
"filter1:value",
"filter2:value"
]

let result = Object.fromEntries(data.map(e => e.split(":")))

console.log(result)


Related Topics



Leave a reply



Submit