severndigital Posted June 17, 2010 Share Posted June 17, 2010 I am getting some information from a json call the json encoded information looks like this. [ {"CUSTOMER_prod_name":"TESTING","J_J_num":"186322R","CUST_CODE_num":"502","JOB_TITLE":"TESTING TITLE","PROOF_REQUIRED":"","LOG_IN_DATE":"10-05-05","DUE_DATE":"10-07-02","DATE_DELIVERED":"","CUSTOMER_PO_NUMBE":"109440","DATE_TEXT_PRINTED":"","JOB_DESCRIPTION":"BOOK","DATE_STRIPPED":"","DATE_PROOF_DELIVERED":"","QTY_ORDERED":"10000","UNIT_PRICE":".48800","LOT_PRICE":"","TOTAL_JOB_COST":"4880.00","REPRINT_TO_PULL":"","REPRINT_PULLED":"","TO_PUNCHING":"05-05-10","PUNCHING_COMPLETED":"","TEXT_TO_OPTI-COPY":"","COVER_TO_OPTI-COPY":"","OPTI-COPY_TEXT_COMPL":"","OPTI-COPY_COVER_COMP":"","TEXT_TO_STRIPPING":"","PROOFS_REQUIRED_TE":"N","PROOFS_OUT_TEXT":"","PROOFS_REQUIRED_CO":"N","PROOFS_BACK_TEXT":"","ANOTHER_PROOF_REQUIR":"","COVER_TO_STRIPPING":"","STRIPPING_TEXT_COMPL":"","JOB_PLATED_TEXT":"06-10-10","PRESS_CODE_TEXT":"442","PROOF_DUE":"","prod_name":"","cust_acctnum":"4","DIGITAL_PRESS":"","DESTCITY,STATE":"","PROOF_BACK":"","DEPOSIT":"","DATE_COVER_PRINTED":"","TEXT_FOLDED":"","COVER_FOLDED":"","BOUND":"","NEW_JOB_Y_or_N":"N","NEW_CODE_num":"N","OLD_CUST_CODE":"502","JOB_PLATED_COVER":"06-15-10","PRESS_CODE_COVER":"640","TEXT_PRINTED":"","STRIPPING_COVER_COMP":"","COVER_PRINTED":"","TEXT_TO_CAMERA":"","FOLD_TEXT":"","COVER_TO_CAMERA":"","FOLD_COVER":"","TEXT_READY_FOR_PLATI":"","COVER_READY_FOR_PLAT":"","RUSH_JOB":"N","TYPE_OF_BINDING":"SWS","GENERAL_DESCRIPTION":"48P+C,_12X9_OB,3C_COV","TEXT_TO_BE_PROOFED":"","BEGIN_STRIPPING_TEXT":"","BEGIN_STRIPPING_COVE":"","PROOFS_OUT_COVER":"","PROOFS_BACK_COVER":"","ANOTHER_PROOF_REQUIR":"","COVER_TO_BE_PROOFED":"","CORRECTIONS_TO_PROOF":"","CORRECTIONS_TO_PROOF":"","num_OF_PROOFS_SENT_T":"","num_OF_PROOFS_SENT_C":"","COD_CUSTOMER":"","DIGITAL_PRINT":"N","DOCUSHARE_UPLOAD":"Y","INVOICED":"","PRO_INV_STATUS":"","FREE":"","FREE":"","ESTIMATE_num":"","PURGE_DIGITAL_FLATS":"N","CSR_INITIALS":"KH","ARRIVAL_DATE":"10-05-05","HOT_LIST":"N","HOT_COMMENT":"","ON_HOLD":"N","ON_HOLD_COMMENT":"","EST_DEL_DATE":"","DEL_Date_Comment":"","ROUTING_CODE":"","PAPER_COST":"","MANUFACTURING_COST":"","FRIEGHT_COST":"","MISC_AA_COST":"","40num_OFFSET_LBS":"","60num_C2S_SHEETS":""}] but with many more entries. when I use the json_decode() function.. i get this. Array ( [0] => stdClass Object ( [CUSTOMER_prod_name] => TESTING [J_J_num] => 186322R [CUST_CODE_num] => 502 [JOB_TITLE] => TESTING TITLE [PROOF_REQUIRED] => [LOG_IN_DATE] => 10-05-05 [DUE_DATE] => 10-07-02 [DATE_DELIVERED] => [CUSTOMER_PO_NUMBE] => 109440 [DATE_TEXT_PRINTED] => [JOB_DESCRIPTION] => BOOK [DATE_STRIPPED] => [DATE_PROOF_DELIVERED] => [QTY_ORDERED] => 10000 [uNIT_PRICE] => .48800 [LOT_PRICE] => [TOTAL_JOB_COST] => 4880.00 [REPRINT_TO_PULL] => [REPRINT_PULLED] => [TO_PUNCHING] => 05-05-10 [PUNCHING_COMPLETED] => [TEXT_TO_OPTI-COPY] => [COVER_TO_OPTI-COPY] => [OPTI-COPY_TEXT_COMPL] => [OPTI-COPY_COVER_COMP] => [TEXT_TO_STRIPPING] => [PROOFS_REQUIRED_TE] => N [PROOFS_OUT_TEXT] => [PROOFS_REQUIRED_CO] => N [PROOFS_BACK_TEXT] => [ANOTHER_PROOF_REQUIR] => [COVER_TO_STRIPPING] => [sTRIPPING_TEXT_COMPL] => [JOB_PLATED_TEXT] => 06-10-10 [PRESS_CODE_TEXT] => 442 [PROOF_DUE] => [prod_name] => [cust_acctnum] => 4 [DIGITAL_PRESS] => [DESTCITY,STATE] => [PROOF_BACK] => [DEPOSIT] => [DATE_COVER_PRINTED] => [TEXT_FOLDED] => [COVER_FOLDED] => [bOUND] => [NEW_JOB_Y_or_N] => N [NEW_CODE_num] => N [OLD_CUST_CODE] => 502 [JOB_PLATED_COVER] => 06-15-10 [PRESS_CODE_COVER] => 640 [TEXT_PRINTED] => [sTRIPPING_COVER_COMP] => [COVER_PRINTED] => [TEXT_TO_CAMERA] => [FOLD_TEXT] => [COVER_TO_CAMERA] => [FOLD_COVER] => [TEXT_READY_FOR_PLATI] => [COVER_READY_FOR_PLAT] => [RUSH_JOB] => N [TYPE_OF_BINDING] => SWS [GENERAL_DESCRIPTION] => 48P+C,_12X9_OB,3C_COV [TEXT_TO_BE_PROOFED] => [bEGIN_STRIPPING_TEXT] => [bEGIN_STRIPPING_COVE] => [PROOFS_OUT_COVER] => [PROOFS_BACK_COVER] => [COVER_TO_BE_PROOFED] => [CORRECTIONS_TO_PROOF] => [num_OF_PROOFS_SENT_T] => [num_OF_PROOFS_SENT_C] => [COD_CUSTOMER] => [DIGITAL_PRINT] => N [DOCUSHARE_UPLOAD] => Y [iNVOICED] => [PRO_INV_STATUS] => [FREE] => [ESTIMATE_num] => [PURGE_DIGITAL_FLATS] => N [CSR_INITIALS] => KH [ARRIVAL_DATE] => 10-05-05 [HOT_LIST] => N [HOT_COMMENT] => [ON_HOLD] => N [ON_HOLD_COMMENT] => [EST_DEL_DATE] => [DEL_Date_Comment] => [ROUTING_CODE] => [PAPER_COST] => [MANUFACTURING_COST] => [FRIEGHT_COST] => [MISC_AA_COST] => [40num_OFFSET_LBS] => [60num_C2S_SHEETS] => ) ); again with many more entries. What i need to do is sort the array entries by information in one of the keys (for this example lets use J_J_num. how would I do that? I tried playing with array_multisort(), but have had no success. any help would be great. Thanks in advance, P Quote Link to comment https://forums.phpfreaks.com/topic/205078-sorting-this-array-information/ Share on other sites More sharing options...
kenrbnsn Posted June 17, 2010 Share Posted June 17, 2010 What do you mean by "What i need to do is sort the array entries by information in one of the keys (for this example lets use J_J_num."? Ken Quote Link to comment https://forums.phpfreaks.com/topic/205078-sorting-this-array-information/#findComment-1073525 Share on other sites More sharing options...
Psycho Posted June 17, 2010 Share Posted June 17, 2010 use the usort() function and create a function for the sorting. http://us.php.net/manual/en/function.usort.php You can even sort by multiple fields.Here is an example with sorting of three different fields. function sortEntries($a, $b) { //If J_J_Num is not same, sort by that field if($a['J_J_num']!=$b['J_J_num']) { return ($a['J_J_num']<$b['J_J_num'])? 1 : -1; } //If J_J_Num was the same, sort by field2, if not the same if($a['field2']!=$b['field2']) { return ($a['J_J_num']<$b['J_J_num'])? 1 : -1; } //If J_J_Num and field2 were the same, sort by field3, if not the same if($a['field3']!=$b['field3']) { return ($a['J_J_num']<$b['J_J_num'])? 1 : -1; } //All sort fields were the same return 0 return 0; } usort($theArray, 'sortEntries'); Note: the order of 1 and -1 determine whether that field is sorted in ascending or descending order. Quote Link to comment https://forums.phpfreaks.com/topic/205078-sorting-this-array-information/#findComment-1073531 Share on other sites More sharing options...
severndigital Posted June 17, 2010 Author Share Posted June 17, 2010 i will simplify the data for now. Hope this helps . the array looks something like this. Array ( [0] => stdClass Object ( [CUSTOMER_prod_name] => TESTING [J_J_num] => 186322R ) [1] => stdClass Object ( [CUSTOMER_prod_name] => TESTING [J_J_num] => 155654 ) [2] => stdClass Object ( [CUSTOMER_prod_name] => TESTING [J_J_num] => 456789 ) ) I need to sort this array by the key J_J_num Quote Link to comment https://forums.phpfreaks.com/topic/205078-sorting-this-array-information/#findComment-1073533 Share on other sites More sharing options...
severndigital Posted June 17, 2010 Author Share Posted June 17, 2010 thanks I was able to get it working using usort(). again thanks. Quote Link to comment https://forums.phpfreaks.com/topic/205078-sorting-this-array-information/#findComment-1073557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.