Hello:

I'm using array_multisort() to attempt to sort my multidimensional array but I can't get it to work right. The examples on php.net don't seem to fit what I'm doing or I can't seem to apply it correctly.

Here's an example of my array:

<?php $stateList = array( 	array ( Location => "Alabama",
																		array( Territory => "Baldwin County",
																					 Publisher => "Midway Publishing, LLC",
																					 Phone => "251-555-1212",
																					 Website => "http://test.com"
																					),
																		array( Territory => "Butler County",
																					 Publisher => "Grey-Wells Publishing",
																					 Phone => "555-555-2121",
																					 Website => "http://test.com"
																					),
																		array( Territory => "Crenshaw County",
																					 Publisher => "Grey-Wells Publishing",
																					 Phone => "334-555-3232",
																					 Website => "http://test.com"
																					)		

Of course, these are currently in ABC order but they won't always be from here on out.

I have a loop ($a) to run through Location (currently only showing Alabama above) and then a loop inside that ($b) to show the counties.

Here's my attempt at array_multisort():

$sortCountyABC = array_multisort($stateList[$a]["Loction"], SORT_ASC);
print_r($sortCountyABC);

Every way I try nothing works!

I get:
Warning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in /home/tidbitsnet/domains/tidbitsweekly.net/public_html/tidbits_locations.php on line 2

Any ideas?

Thanks!

    For starters, the structure of your array is not clear since your example is not syntactically correct.

    Would this be a correct example?

    $stateList = array(
        array(
            'Territory' => "Baldwin County",
            'Publisher' => "Midway Publishing, LLC",
            'Phone' => "251-555-1212",
            'Website' => "http://test.com"
        ),
        array(
            'Territory' => "Butler County",
            'Publisher' => "Grey-Wells Publishing",
            'Phone' => "555-555-2121",
            'Website' => "http://test.com"
        ),
        array(
            'Territory' => "Crenshaw County",
            'Publisher' => "Grey-Wells Publishing",
            'Phone' => "334-555-3232",
            'Website' => "http://test.com"
        )
    );

      Not really. I didn't include the entire array for the sake of space and was hoping to put just enough to show my intentions. I will go ahead and post the entire thing:

      <?php $stateList = array( 	array ( Location => "Alabama",
        array( Territory => "Baldwin County",
      				 Publisher => "Midway Publishing, LLC",
      				 Phone => "251-947-4648",
      				 Website => "http://tidbitsofbaldwincounty.com"
      				),
      	array( Territory => "Butler County",
      				 Publisher => "Grey-Wells Publishing",
      				 Phone => "334-382-6262",
      				 Website => "http://www.tidbitsweekly.com/publisher/tidbitsofgreenville_and_montgomery"
      				),
      	array( Territory => "Crenshaw County",
      				 Publisher => "Grey-Wells Publishing",
      				 Phone => "334-382-6262",
      				 Website => "http://www.tidbitsweekly.com/publisher/tidbitsofgreenville_and_montgomery"
      				),																					
      	array( Territory => "Lowndes County",
      				 Publisher => "Grey-Wells Publishing",
      				 Phone => "334-382-6262",
      				 Website => "http://www.tidbitsweekly.com/publisher/tidbitsofgreenville_and_montgomery"
      				),
      	array( Territory => "Pike County",
      				 Publisher => "Grey-Wells Publishing",
      				 Phone => "334-382-6262",
      				 Website => "http://www.tidbitsweekly.com/publisher/tidbitsofgreenville_and_montgomery"
      				),
      	array( Territory => "Lee County",
      				 Publisher => "Path Consulting Group",
      				 Phone => "334-524-8883",
      				 Website => "http://tidbitsofbaldwincounty.com"
      				),
      	array( Territory => "Montgomery County",
      				 Publisher => "Grey-Wells Publishing",
      				 Phone => "334-382-6262",
      				 Website => "http://www.tidbitsweekly.com/publisher/tidbitsofgreenville_and_montgomery"
      				),	
      	array( Territory => "Russell County",
      				 Publisher => "Path Consulting Group",
      				 Phone => "334-524-8883",
      				 Website => ""
      				)																																																																																																																																													
      ),
      array ( Location => "Alaska" ),
      array ( Location => "Arizona" ),
      array ( Location => "Arkansas" ),
      array ( Location => "California" ),
      array ( Location => "Colorado" ),
      array ( Location => "Connecticut" ),
      array ( Location => "Delaware" ),
      array ( Location => "Florida" ),
      array ( Location => "Georgia" ),
      array ( Location => "Hawaii" ),
      array ( Location => "Idaho" ),
      array ( Location => "Illinois" ),
      array ( Location => "Indiana" ),
      array ( Location => "Iowa" ),
      array ( Location => "Kansas " ),
      array ( Location => "Kentucky " ),
      array ( Location => "Louisiana" ),
      array ( Location => "Maine" ),
      array ( Location => "Maryland" ),
      array ( Location => "Massachusetts" ), 
      array ( Location => "Michigan" ),
      array ( Location => "Minnesota" ),
      array ( Location => "Mississippi" ),
      array ( Location => "Missouri" ),
      array ( Location => "Montana" ),
      array ( Location => "Nebraska" ),
      array ( Location => "Nevada" ),
      array ( Location => "New Hampshire" ),
      array ( Location => "New Jersey" ),
      array ( Location => "New Mexico" ),
      array ( Location => "New York" ),
      array ( Location => "North Carolina" ),
      array ( Location => "North Dakota" ),
      array ( Location => "Ohio" ),
      array ( Location => "Oklahoma" ),
      array ( Location => "Oregon" ),
      array ( Location => "Pennsylvania" ),
      array ( Location => "Rhode Island" ),
      array ( Location => "South Carolina" ),
      array ( Location => "South Dakota" ),
      array ( Location => "Tennessee" ),
      array ( Location => "Texas" ),
      array ( Location => "Utah" ),
      array ( Location => "Vermont" ),
      array ( Location => "Virginia" ),
      array ( Location => "Washington" ),
      array ( Location => "West Virginia" ),
      array ( Location => "Wisconsin" ),
      array ( Location => "Wyoming" )
      						)
      ?>
      

        I found the following on the array_multisort function page at php.net and it works fine for me:

        function sortmddata($array, $by, $order, $type){
        
        //$array: the array you want to sort
        //$by: the associative array name that is one level deep
        ////example: name
        //$order: ASC or DESC
        //$type: num or str
        
        $sortby = "sort$by"; //This sets up what you are sorting by
        
        $firstval = current($array); //Pulls over the first array
        
        $vals = array_keys($firstval); //Grabs the associate Arrays
        
        foreach ($vals as $init){
            $keyname = "sort$init";
            $$keyname = array();
        }
        //This was strange because I had problems adding
        //Multiple arrays into a variable variable
        //I got it to work by initializing the variable variables as arrays
        //Before I went any further
        
        foreach ($array as $key => $row) {
        
        foreach ($vals as $names){
            $keyname = "sort$names";
            $test = array();
            $test[$key] = $row[$names];
            $$keyname = array_merge($$keyname,$test);
        
        }
        
        }
        
        //This will create dynamic mini arrays so that I can perform
        //the array multisort with no problem
        //Notice the temp array... I had to do that because I
        //cannot assign additional array elements to a
        //varaiable variable           
        
        if ($order == "DESC"){   
        if ($type == "num"){ array_multisort($$sortby,SORT_DESC, SORT_NUMERIC,$array); } else { array_multisort($$sortby,SORT_DESC, SORT_STRING,$array); } } else { if ($type == "num"){ array_multisort($$sortby,SORT_ASC, SORT_NUMERIC,$array); } else { array_multisort($$sortby,SORT_ASC, SORT_STRING,$array); } } //This just goed through and asks the additional arguments //What they are doing and are doing variations of //the multisort return $array; }

        Example of usage:

        //Here is an array example
        $test[0]['name'] = "David";
        $test[0]['age'] = 28;
        $test[1]['name'] = "Dennis";
        $test[1]['age'] = 23;
        $test[2]['name'] = "Joseph";
        $test[2]['age'] = 42;
        
        $test = sortmddata($test,'age','ASC','num');
        
        print_r ($test);
        
        //This will return
        //Array (
        //[0] => Array ([name] => Dennis [age] => 23 )
        //[1] => Array ( [name] => David [age] => 28 )
        //[2] => Array ( [name] => Joseph [age] => 42 )
        //)
        
          Write a Reply...