hsaenz12 Posted October 19, 2009 Share Posted October 19, 2009 I have two products that I want to sort by say "Id:17, value: xxx" using php The page will end up looking like this Part Number Diameter Effective focal back focal cntr thickness edge thickness L-AOC000 6.00 10.00 7.52 4.75 3.5 here is the decoded json for the two products ( there are a few hundred products) {"id":"16","value":"L-AOC000"},{"id":"17","value":"6.00"},{"id":"18","value":"10.00"},{"id":"19","value":"7.52"},{"id":"20","value":"4.75"},{"id":"21","value":"3.50"} {"id":"16","value":"L-AOC001"},{"id":"17","value":"7.00"},{"id":"18","value":"11.00"},{"id":"19","value":"8.52"},{"id":"20","value":"3.75"},{"id":"21","value":"2.50"} any help would be appreciated Link to comment https://forums.phpfreaks.com/topic/178283-json-sort-with-php/ Share on other sites More sharing options...
trq Posted October 19, 2009 Share Posted October 19, 2009 Where exactly are you stuck? Link to comment https://forums.phpfreaks.com/topic/178283-json-sort-with-php/#findComment-940049 Share on other sites More sharing options...
hsaenz12 Posted October 19, 2009 Author Share Posted October 19, 2009 Well I'm trying to hack this program i have (open source). A a point in a function the script returns the values that i posted above. i want to be able to sort those values before the function strips the strings further. I am somewhat new to programming (teaching myself) but i can understand it. here is what the function looks like elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) { // array, or object notation if ($str{0} == '[') { $stk = array(SERVICES_JSON_IN_ARR); $arr = array(); } else { if ($this->use & SERVICES_JSON_LOOSE_TYPE) { $stk = array(SERVICES_JSON_IN_OBJ); $obj = array(); } else { $stk = array(SERVICES_JSON_IN_OBJ); $obj = new stdClass(); } } array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => 0, 'delim' => false)); $chrs = substr($str, 1, -1); $chrs = $this->reduce_string($chrs); if ($chrs == '') { if (reset($stk) == SERVICES_JSON_IN_ARR) { return $arr; } else { return $obj; } } //print("$chrs");<-------- at this point is where I get the strings i posted above. is it even possible to sort at this point? Link to comment https://forums.phpfreaks.com/topic/178283-json-sort-with-php/#findComment-940104 Share on other sites More sharing options...
mikesta707 Posted October 19, 2009 Share Posted October 19, 2009 if its valid json, than you can just use json_decode to turn it into a PHP variable, sort it however you like, and then re-encode it if you need to Link to comment https://forums.phpfreaks.com/topic/178283-json-sort-with-php/#findComment-940106 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.