0

the php json_encode($object) result is

[{"price_id":"1","website_id":"0","price_qty":2,"price":"90.0000"},
 {"price_id":"2","website_id":"0","price_qty":5,"price":"80.0000"},
 {"price_id":"3","website_id":"0","price_qty":8,"price":"70.0000"}]

someone tell me, var sorted = arrayWithJsonData.sort(function(a,b){}

how to use the above php json_encode($object) to arrayWithJsonData. i used the following way, but it shows TypeError: object.sort is not a function

first: in php i did: echo '<div style="display:none;" id="object">'.$object.'</div>'; then using in jquery

var object=jQuery("#object").text(); // first sort the array var sorted = object.sort(function(a,b){}
1
  • I think the error TypeError: object.sort is not a function is from the javascript object arrayWithJsonData, not php Commented Nov 23, 2012 at 15:30

3 Answers 3

1

You need to parse the object first so javascript(jQuery) can understand it:

JSON.parse(data);
Sign up to request clarification or add additional context in comments.

Comments

1
 var arrayWithJsonData = $.parseJSON($('#object').text());

$.parseJSON is a jQuery utitlity for converting a well-formed json string to a json object (jQuery doc: http://api.jquery.com/jQuery.parseJSON/). In our case, $('#object').text() method of the matched element returns the well-formed json string which is the output of the PHP function json_encode($object). Hope that helps.

1 Comment

Perhaps you could explain your answer a little to help the OP understand why it works.
0
var arrayWithJsonData = <?php json_encode($object) ?>;
var sorted = arrayWithJsonData.sort(function(a, b){
    // Sort criteria between a and b
});

Comments

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.