@@ -547,52 +547,74 @@ static public function isArraySame(?array $data = [])
547547 * @param string $type
548548 * - [rsort|asort|ksort|arsort|krsort|sort]
549549 *
550- * @return array
550+ * @return void
551551 */
552- static public function sortArray (?array $ data = [], ?string $ type = 'sort ' )
552+ static public function sortArray (?array & $ data = [], ?string $ type = 'sort ' )
553553 {
554+ // Validate that $data is an array
555+ if (!is_array ($ data )) {
556+ return ;
557+ }
558+
559+ // Perform sorting based on the specified type
554560 switch ($ type ) {
555561 case 'rsort ' :
556- rsort ($ data ); // sort arrays in descending order
562+ rsort ($ data ); // Sort arrays in descending order
557563 break ;
558-
564+
559565 case 'asort ' :
560- asort ($ data ); // sort associative arrays in ascending order, according to the value
566+ asort ($ data ); // Sort associative arrays in ascending order, according to the value
561567 break ;
562-
568+
563569 case 'ksort ' :
564- ksort ($ data ); // sort associative arrays in ascending order, according to the key
570+ ksort ($ data ); // Sort associative arrays in ascending order, according to the key
565571 break ;
566-
572+
567573 case 'arsort ' :
568- arsort ($ data ); // sort associative arrays in descending order, according to the value
574+ arsort ($ data ); // Sort associative arrays in descending order, according to the value
569575 break ;
570-
576+
571577 case 'krsort ' :
572- krsort ($ data ); // sort associative arrays in descending order, according to the value
578+ krsort ($ data ); // Sort associative arrays in descending order, according to the value
573579 break ;
574-
580+
575581 default :
576- sort ($ data ); // sort arrays in ascending order
582+ sort ($ data ); // Sort arrays in ascending order
577583 break ;
578584 }
579-
580- return $ data ;
581585 }
582586
583587 /**
584588 * For sorting muti-dimentional array
585589 *
586- * @param string|null $key
587590 * @param array $data
591+ * @param string|null $key
588592 * @param string $type
589593 * - [asc|desc|snum]
590594 *
591595 * @return void
592596 */
593- static public function sortMultipleArray ($ key = null , ?array &$ data = [], ?string $ type = 'asc ' )
597+ static public function sortMultipleArray (?array &$ data = [], $ key = null , ?string $ type = 'asc ' )
594598 {
599+ // Check if $data is an array and not empty
600+ if (!is_array ($ data ) || empty ($ data )) {
601+ return ;
602+ }
603+
604+ // Check if $key is provided
605+ if ($ key === null ) {
606+ // If $key is not provided, return without sorting
607+ return ;
608+ }
609+
610+ // Extract values of the specified key from each sub-array
595611 $ id = array_column ($ data , $ key );
612+
613+ // Ensure $id and $data have the same size before sorting
614+ if (count ($ id ) !== count ($ data )) {
615+ return ;
616+ }
617+
596618 switch ($ type ) {
597619 case 'desc ' :
598620 array_multisort ($ id , SORT_DESC , $ data ); //sort associative arrays in descending order
@@ -831,10 +853,8 @@ static public function convertJsonData($path, $format = true)
831853 */
832854 static public function saveDataAsJsonObject (string $ destination , mixed $ data , ?bool $ type = true )
833855 {
834- $ format = JSON_PRETTY_PRINT ;
835- if (!$ type ){
836- $ format = JSON_UNESCAPED_UNICODE ;
837- }
856+ // Choose the JSON encoding format
857+ $ format = $ type ? JSON_PRETTY_PRINT : JSON_UNESCAPED_UNICODE ;
838858
839859 // check or convert data to an array
840860 if (!is_array (!$ data )){
0 commit comments