Jump to content

What can I do with this string?


forumnz

Recommended Posts

Hey

 

I have a JS script that POSTs this:

[{"field3":66,"field4":5,"field1":"ggg","field2":"g","field5":"h","field6":"h","field7":"hh","id":"1"},{"field2":"cgh","field4":73377,"field3":770}]

to a PHP file.

 

What can I do to insert that information into a database?

Note that the data between {} is it's own row.

 

Basically, how can I get that into some sort of suitable array?

Link to comment
Share on other sites

I thought you wanted to store them in the DB.

 

Look -

$e = '{"field3":66,"field4":5,"field1":"ggg","field2":"g","field5":"h","field6":"h","field7":"hh","id":"1"}';
var_dump(json_decode($e, true));

 

You don't like the output of that? It's an array you should be familiar with.

Link to comment
Share on other sites

This is what I have for the quotes problem:

<?php
$str = $data;
$str = substr($str, 1, strlen($str) - 2);

$str = preg_replace("/([a-zA-Z0-9_]+?):/" , "\"$1\":", $str); // fix left side
$str = preg_replace("/:(^false^true[a-zA-Z_]+[a-zA-Z0-9_]*)/", ":\"$1\"", $str); // fix right side 
  
  
$data = json_decode($str, true);


$data = print_r($data);
?>

 

Still returns 1 though..?

Why would that be?

 

I do want to store them in a database but I need to get the data into an array for that don't I?

Link to comment
Share on other sites

Thanks Ken2k7

 

I modified one of your lines to read:

<?php
$e = '[{"field3":66,"field4":5,"field1":"ggg","field2":"g","field5":"h","field6":"h","field7":"hh","id":"1"},{"field3":66,"field4":5,"field1":"ggg","field2":"g","field5":"h","field6":"h","field7":"hh","id":"1"}]';
?>

Which is basically 2 sets of data.

 

And the output is:

array(2) { [0]=>  array( { ["field3"]=>  int(66) ["field4"]=>  int(5) ["field1"]=>  string(3) "ggg" ["field2"]=>  string(1) "g" ["field5"]=>  string(1) "h" ["field6"]=>  string(1) "h" ["field7"]=>  string(2) "hh" ["id"]=>  string(1) "1" } [1]=>  array( { ["field3"]=>  int(66) ["field4"]=>  int(5) ["field1"]=>  string(3) "ggg" ["field2"]=>  string(1) "g" ["field5"]=>  string(1) "h" ["field6"]=>  string(1) "h" ["field7"]=>  string(2) "hh" ["id"]=>  string(1) "1" } } 

 

Which is fine (I believe). What would I need to do now?

 

Much appreciated :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.