r2k1g
r2k1g

Reputation: 15

Convert PHP array format to js array format, using js only

I know there's a json_encode on php. currently we are using that. But it's the only thing that slows the app down cause it needs to go to the server.

Here's the situation, we 2 apps. Let's call them app1 and app2.

App1 is a big application. One of it's input is a php array.

Now app2 on the other hand is a small application created to help app1. App2 has a form. The form has many many fields. Then all of these fields will be converted to the correct php array format, ready to be used as an input in app1.

Now our problem, every time we use app2, we should be more careful or always keep the fields. Cause if there's one field with wrong info, we need to do it again, use app2 to get the correct array.

So, we think we need to add a feature on app2. We want it to be able to edit the php array it produce. We've done that. When php array is given, we send it to server, use json_encode, then put everything in place. Now we have all the fields populated and we can then edit the fields with incorrect data.

At the moment, we are okay with that, but I think it would be more efficient to not go to the server and just convert the php array using js... I've searched google and stackoverflow, I found nothing. I did try using .replace(), but I haven't figured it out yet a good result..

I'm hoping someone can help me here..

EDIT:

it's a PHP array format by the way...

as simple example we have to input fields. After filling out the fields it will output a PHP array format in textarea.

Upvotes: 1

Views: 275

Answers (2)

Walter Caraza
Walter Caraza

Reputation: 911

you can convert a json format under

$array = array("hola","hello");
exit(json_encode($array));



output json // {"hola","hello"}

in js

sucess(x){

}

Upvotes: 0

nnnnnn
nnnnnn

Reputation: 150020

From your comment, you seem to be saying you have a string in a textarea like this:

<textarea>array('a'=>'goat', 'b' => 'dog')</textarea>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

I really don't understand why your app would be putting any "code" type result in a textarea in the first place, let alone a string that is a PHP format array literal, but to attempt to answer your question about how to use JavaScript to turn such a string into a JS object, you can do this:

// assuming your textarea has the id "myTextArea":
var str = document.getElementById("myTextArea").value,
    json = "{" + str.slice(6, -1).replace(/=>/g,":").replace(/'/g,"\"") + "}";
console.log(json);
var obj = JSON.parse(json);
console.log(obj);

Demo: http://jsfiddle.net/WknYX/

I put the two console.log() statements in so that you can see what the string looks like after it's been manipulated into JSON format, and then see the resulting object once that JSON has been parsed.

Note that obviously this code will break with an error if the string is in any other format, and if the items in the array contain the characters that are being replaced that would not give an error but would give incorrect results.

Upvotes: 1

Related Questions