0

Hi i have that php code example :

$array1 = array("fruits" => "banana","vegetables" => "tomatos");
$array2 = array("name" => "Jack","Age" => "32");

$array3 = array($array1, $array2);

echo json_encode($array3);

javascript code includes jquery :

   var json_data = $.ajax({
                type: 'POST',
                url: 'scripts/myfile.php',
                data: { action: 'myaction' },
                dataType: 'json',
                cache: false,
                success: function(result) {
                    console.log(result);
                }
            });

how to convert json_data to an array and affect the two of $array1 & $array2 to javascript arrays !

this is the json_data content :

"{"HUM":[{"label":"2014-10-16 17:08:55","y":"58"},{"label":"2014-10-15 08:16:55","y":"56"},{"label":"2014-10-15 08:16:50","y":"56"},{"label":"2014-10-15 08:16:45","y":"56"},{"label":"2014-10-15 08:16:40","y":"56"},{"label":"2014-10-15 08:16:35","y":"56"},{"label":"2014-10-15 08:16:30","y":"56"},{"label":"2014-10-15 08:16:25","y":"56"},{"label":"2014-10-15 08:16:20","y":"56"},{"label":"2014-10-15 08:16:15","y":"56"},

"TEMP":[{"label":"2014-10-16 17:08:55","y":"26"},{"label":"2014-10-15 08:16:55","y":"24"},{"label":"2014-10-15 08:16:50","y":"24"},{"label":"2014-10-15 08:16:45","y":"24"},{"label":"2014-10-15 08:16:40","y":"24"},{"label":"2014-10-15 08:16:35","y":"24"},{"label":"2014-10-15 08:16:30","y":"24"},{"label":"2014-10-15 08:16:25","y":"24"},{"label":"2014-10-15 08:16:20","y":"24"},{"label":"2014-10-15 08:16:15","y":"24"},

"HUM2":[{"label":"2014-10-16 17:08:55","y":"38"},{"label":"2014-10-15 08:16:55","y":"36"},{"label":"2014-10-15 08:16:50","y":"36"},{"label":"2014-10-15 08:16:45","y":"36"},{"label":"2014-10-15 08:16:40","y":"36"},{"label":"2014-10-15 08:16:35","y":"36"},{"label":"2014-10-15 08:16:30","y":"36"},{"label":"2014-10-15 08:16:25","y":"36"},{"label":"2014-10-15 08:16:20","y":"36"},{"label":"2014-10-15 08:16:15","y":"36"},{"label":"2014-10-15 08:16:10","y":"36"},

"TEMP2":[{"label":"2014-10-16 17:08:55","y":"23"},{"label":"2014-10-15 08:16:55","y":"24"},{"label":"2014-10-15 08:16:50","y":"24"},{"label":"2014-10-15 08:16:45","y":"24"},{"label":"2014-10-15 08:16:40","y":"24"},{"label":"2014-10-15 08:16:35","y":"24"},{"label":"2014-10-15 08:16:30","y":"24"},{"label":"2014-10-15 08:16:25","y":"24"},{"label":"2014-10-15 08:16:20","y":"24"},{"label":"2014-10-15 08:16:15","y":"24"},{"label":"2014-10-15 08:16:10","y":"24"},{"label":"2014-10-15 08:16:05","y":"24"},{"label":"2014-10-15 08:16:00","y":"24"}]}"

now i want to convert it to array and then serparate arrays HUM - TEMP - HUM2 - TEMP2

8
  • obj = JSON.parse(result); Commented Oct 17, 2014 at 11:48
  • obj = $.parseJSON(result); Commented Oct 17, 2014 at 11:50
  • it gives that error : Uncaught SyntaxError: Unexpected token o Commented Oct 17, 2014 at 11:52
  • json_data in your example is a jqXHR object. You need to access the data in the success callback: success: function(result) {alert(result[1].name);//alerts Jack}, Commented Oct 17, 2014 at 11:57
  • You should remove the "," after the success function. Do I understand you right, you want to get one array back that is a merge of <?php $array1 and $array2 ?> that would be with <?php $array3 = array_merge($array1, $array2); ?> Commented Oct 17, 2014 at 11:58

3 Answers 3

1

In your code,

$array3 = array(array1, array2);

here array1 and array2 considered as string without quotes, so you will not get your answer. This should be PHP variable. You have to include $ sign in this.

$array3 = array($array1, $array2);
0

$array3 = array(array1, array2); should be $array3 = array($array1, $array2);

0

From jQuery $.ajax() documentation on dataType when it is set to json.

"json": Evaluates the response as JSON and returns a JavaScript object. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

The object result is already a JSON object in the success function. So you can simply use result[0]['fruits'] or result[1]['name'] to access the relevant arrays.

Edit : Corrected index as @charlietfl pointed out.

1
  • incorrect regarding result['fruits'] , the way OP is merging arrays of arrays ....would be result[0]['fruits'] or result[1]['name'] Commented Oct 17, 2014 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.