We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4002
    • 64 Posts
    i have an associative array output from using getDocumentChildrenTVarOutput and i need to sort the resulting array by one of the TV fields (mission_date). my resulting array looks like this:

    Array
    (
        [0] => Array
            (
                [mission_team_members] => 0
                [mission_operations] => 78
                [mission_date] => 
                [id] => 62
                [menutitle] => Memphis, TN, USA
            )
    
        [1] => Array
            (
                [mission_team_members] => 9
                [mission_operations] => 14
                [mission_date] => 
                [id] => 63
                [menutitle] => Zagreb, Croatia
            )
    
        [2] => Array
            (
                [mission_team_members] => 14
                [mission_operations] => 26
                [mission_date] => 
                [id] => 64
                [menutitle] => Zagreb, Croatia
            )
    


    how can i sort this array by mission_date?

    thanks
      • 18397
      • 3,250 Posts
      You might want to give this Ditto function a try:

      <?php
      	// ---------------------------------------------------
      	// Function: customSort
      	// Sort resource array if advanced sorting is needed
      	// ---------------------------------------------------
      
      	function customSort($data, $fields, $order) {
      		// Covert $fields string to array
      		// user contributed
      		foreach (explode(',', $fields) as $s)
      			$sortfields[] = trim($s);
      
      		$code = "";
      		for ($c = 0; $c < count($sortfields); $c++)
      			$code .= "\$retval = strnatcmp(\$a['$sortfields[$c]'], \$b['$sortfields[$c]']); if(\$retval) return \$retval; ";
      		$code .= "return \$retval;";
      
      		$params = ($order == 'ASC') ? '$a,$b' : '$b,$a';
      		uasort($data, create_function($params, $code));
      		return $data;
      	}