Jump to content
dodgeitorelse3

sort multi-dimensional array by name

Recommended Posts

I have an array that is created using the following code:

$mapname='mapname1';
	$type="pvp";
	$beta="y";
	$final="n";
	$modded="n";
	$classification="land";
	$sf="n";
	$tod="dawn";
	$weather="fog";
	$es2="y";

		$mapname2='mapname2';
		$type2="pvp";
		$beta2="n";
		$final2="y";
		$modded2="n";
		$classification2="sea";
		$sf2="n";
		$tod2="night";
		$weather2="clear";
		$es22="n";

			$mapname3='mapname3';
			$type3="pvp";
			$beta3="y";
			$final3="n";
			$modded3="y";
			$classification3="air";
			$sf3="y";
			$tod3="day";
			$weather3="rain";
			$es23="n";

$tadminlist["pvp"] = array ( 
);

array_push($tadminlist["pvp"], array ("name" => $mapname, "type" => $type, "beta" => $beta, "final" => $final, "modded" => $modded, "classification" => $classification, "sf" => $sf, "tod" => $tod, "weather" => $weather, "es2" => $es2));  

array_push($tadminlist["pvp"], array ("name" => $mapname2, "type" => $type2, "beta" => $beta2, "final" => $final2, "modded" => $modded2, "classification" => $classification2, "sf" => $sf2, "tod" => $tod2, "weather" => $weather2, "es2" => $es22));  

array_push($tadminlist["pvp"], array ("name" => $mapname3, "type" => $type3, "beta" => $beta3, "final" => $final3, "modded" => $modded3, "classification" => $classification3, "sf" => $sf3, "tod" => $tod3, "weather" => $weather3, "es2" => $es23));  
echo "<pre>";
print_r($tadminlist);  
echo "</pre>";
	echo "line = mapname, type, beta, final, modded, classification, sf, tod, weather, es2<br /><br />";

		$i =0;

		foreach($tadminlist["pvp"] as $key => $tlist)
		{
			echo $tadminlist['pvp'][$i]['name'].", ".
				$tadminlist['pvp'][$i]['type'].", ".
				$tadminlist['pvp'][$i]['beta'].", ".
				$tadminlist['pvp'][$i]['final'].", ".
				$tadminlist['pvp'][$i]['modded'].", ".
				$tadminlist['pvp'][$i]['classification'].", ".
				$tadminlist['pvp'][$i]['sf'].", ".
				$tadminlist['pvp'][$i]['tod'].", ".
				$tadminlist['pvp'][$i]['weather'].", ".
				$tadminlist['pvp'][$i]['es2']."<br /><br />";
				
				$i++;
		}

The output is:

Array
(
    [pvp] => Array
        (
            [0] => Array
                (
                    [name] => mapname1
                    [type] => pvp
                    [beta] => y
                    [final] => n
                    [modded] => n
                    [classification] => land
                    [sf] => n
                    [tod] => dawn
                    [weather] => fog
                    [es2] => y
                )

            [1] => Array
                (
                    [name] => mapname2
                    [type] => pvp
                    [beta] => n
                    [final] => y
                    [modded] => n
                    [classification] => sea
                    [sf] => n
                    [tod] => night
                    [weather] => clear
                    [es2] => n
                )

            [2] => Array
                (
                    [name] => mapname3
                    [type] => pvp
                    [beta] => y
                    [final] => n
                    [modded] => y
                    [classification] => air
                    [sf] => y
                    [tod] => day
                    [weather] => rain
                    [es2] => n
                )

        )

)

line = mapname, type, beta, final, modded, classification, sf, tod, weather, es2

mapname1, pvp, y, n, n, land, n, dawn, fog, y

mapname2, pvp, n, y, n, sea, n, night, clear, n

mapname3, pvp, y, n, y, air, y, day, rain, n

I would like to sort the foreach loop by name in descending order. I read the page at http://php.net/manual/en/function.array-multisort.php but was unable to figure it out. Could someone give me some direction please?

Edited by dodgeitorelse3
added output

Share this post


Link to post
Share on other sites

It's easier to use usort() instead.

Write a function that takes two parameters, which are the inner arrays, and decides which should sort before the other. Hint: strcmp.

function($a, $b) {
	// return -1 if $a sorts before $b
	// return 0 if they are the same
	// return 1 if $b sorts before $a
}

You'd pass that function to usort when sorting the $tadminlist["pvp"] array.

  • Great Answer 1

Share this post


Link to post
Share on other sites

Thank you, I will look into that. It may be easier than this. I went back to the php page I linked to and tried a sample that was there and got the result I was looking for. Now I have a headache :(

$mapname='mapname1';
	$type="pvp";
	$beta="y";
	$final="n";
	$modded="n";
	$classification="land";
	$sf="n";
	$tod="dawn";
	$weather="fog";
	$es2="y";

		$mapname2='mapname2';
		$type2="pvp";
		$beta2="n";
		$final2="y";
		$modded2="n";
		$classification2="sea";
		$sf2="n";
		$tod2="night";
		$weather2="clear";
		$es22="n";

			$mapname3='mapname3';
			$type3="pvp";
			$beta3="y";
			$final3="n";
			$modded3="y";
			$classification3="air";
			$sf3="y";
			$tod3="day";
			$weather3="rain";
			$es23="n";

$tadminlist["pvp"] = array ( 
);

array_push($tadminlist["pvp"], array ("name" => $mapname, "type" => $type, "beta" => $beta, "final" => $final, "modded" => $modded, "classification" => $classification, "sf" => $sf, "tod" => $tod, "weather" => $weather, "es2" => $es2));  

array_push($tadminlist["pvp"], array ("name" => $mapname2, "type" => $type2, "beta" => $beta2, "final" => $final2, "modded" => $modded2, "classification" => $classification2, "sf" => $sf2, "tod" => $tod2, "weather" => $weather2, "es2" => $es22));  

array_push($tadminlist["pvp"], array ("name" => $mapname3, "type" => $type3, "beta" => $beta3, "final" => $final3, "modded" => $modded3, "classification" => $classification3, "sf" => $sf3, "tod" => $tod3, "weather" => $weather3, "es2" => $es23));  
echo "<pre>";
print_r($tadminlist);  
echo "</pre>";
	echo "line = mapname, type, beta, final, modded, classification, sf, tod, weather, es2<br /><br />";

		$i =0;

		foreach($tadminlist["pvp"] as $tlist)
		{
			echo $tadminlist['pvp'][$i]['name'].", ".
				$tadminlist['pvp'][$i]['type'].", ".
				$tadminlist['pvp'][$i]['beta'].", ".
				$tadminlist['pvp'][$i]['final'].", ".
				$tadminlist['pvp'][$i]['modded'].", ".
				$tadminlist['pvp'][$i]['classification'].", ".
				$tadminlist['pvp'][$i]['sf'].", ".
				$tadminlist['pvp'][$i]['tod'].", ".
				$tadminlist['pvp'][$i]['weather'].", ".
				$tadminlist['pvp'][$i]['es2']."<br /><br />";
				
				$i++;
		}
		
	echo "sort test<br />";
	
	$data = $tadminlist["pvp"];	
	
		function array_orderby()
		{
			$args = func_get_args();
			$data = array_shift($args);
			foreach ($args as $n => $field) {
				if (is_string($field)) {
					$tmp = array();
					foreach ($data as $key => $row)
						$tmp[$key] = $row[$field];
					$args[$n] = $tmp;
					}
			}
			$args[] = &$data;
			call_user_func_array('array_multisort', $args);
			return array_pop($args);
		}

		// Pass the array, followed by the column names and sort flags
		$sorted = array_orderby($data, 'name', SORT_DESC);
		
		$a=0;
		
		foreach($sorted as $tlist)
		{
			echo $sorted[$a]['name'].", ".
				$sorted[$a]['type'].", ".
				$sorted[$a]['beta'].", ".
				$sorted[$a]['final'].", ".
				$sorted[$a]['modded'].", ".
				$sorted[$a]['classification'].", ".
				$sorted[$a]['sf'].", ".
				$sorted[$a]['tod'].", ".
				$sorted[$a]['weather'].", ".
				$sorted[$a]['es2']."<br /><br />";
				
				$a++;
		}
		
		echo "<pre>";
		print_r($sorted);
		echo "</pre>";

and my output is

Array
(
    [pvp] => Array
        (
            [0] => Array
                (
                    [name] => mapname1
                    [type] => pvp
                    [beta] => y
                    [final] => n
                    [modded] => n
                    [classification] => land
                    [sf] => n
                    [tod] => dawn
                    [weather] => fog
                    [es2] => y
                )

            [1] => Array
                (
                    [name] => mapname2
                    [type] => pvp
                    [beta] => n
                    [final] => y
                    [modded] => n
                    [classification] => sea
                    [sf] => n
                    [tod] => night
                    [weather] => clear
                    [es2] => n
                )

            [2] => Array
                (
                    [name] => mapname3
                    [type] => pvp
                    [beta] => y
                    [final] => n
                    [modded] => y
                    [classification] => air
                    [sf] => y
                    [tod] => day
                    [weather] => rain
                    [es2] => n
                )

        )

)

line = mapname, type, beta, final, modded, classification, sf, tod, weather, es2

mapname1, pvp, y, n, n, land, n, dawn, fog, y

mapname2, pvp, n, y, n, sea, n, night, clear, n

mapname3, pvp, y, n, y, air, y, day, rain, n

sort test
mapname3, pvp, y, n, y, air, y, day, rain, n

mapname2, pvp, n, y, n, sea, n, night, clear, n

mapname1, pvp, y, n, n, land, n, dawn, fog, y

Array
(
    [0] => Array
        (
            [name] => mapname3
            [type] => pvp
            [beta] => y
            [final] => n
            [modded] => y
            [classification] => air
            [sf] => y
            [tod] => day
            [weather] => rain
            [es2] => n
        )

    [1] => Array
        (
            [name] => mapname2
            [type] => pvp
            [beta] => n
            [final] => y
            [modded] => n
            [classification] => sea
            [sf] => n
            [tod] => night
            [weather] => clear
            [es2] => n
        )

    [2] => Array
        (
            [name] => mapname1
            [type] => pvp
            [beta] => y
            [final] => n
            [modded] => n
            [classification] => land
            [sf] => n
            [tod] => dawn
            [weather] => fog
            [es2] => y
        )

)

Now to take what I need and put it into my actual page of code where it is to be used.

I will post back when I test what you suggested. Thank you for the direction (and hint) as well as your time.

Share this post


Link to post
Share on other sites

As you are sorting on "name", which is the first element of the sub-arrays, you can just use an ordinay sort() or rsort() call. (By default it will sort on the values oof the first element)

EG

$tadminlist["pvp"] = [
                        [ 'name' => 'mapname1',
                          'type' => 'pvp',
                          'beta' => 'y',
                          'final' => 'n',
                          'modded' => '',
                          'classification' => 'land',
                          'sf' => 'n',
                          'tod' => 'dawn',
                          'weather' => 'fog',
                          'es2' => 'y'
                        ],
                        [ 'name' => 'mapname3',
                          'type' => 'pvp',
                          'beta' => 'yy',
                          'final' => 'n',
                          'modded' => 'y',
                          'classification' => 'air',
                          'sf' => 'y',
                          'tod' => 'day',
                          'weather' => 'rain',
                          'es2' => 'n'
                        ],
                        [ 'name' => 'mapname2',
                          'type' => 'pvp',
                          'beta' => 'n',
                          'final' => 'y',
                          'modded' => 'n',
                          'classification' => 'sea',
                          'sf' => 'n',
                          'tod' => 'night',
                          'weather' => 'clear',
                          'es2' => 'n'
                        ],
                    ];


echo "line = " . join(', ', array_keys($tadminlist["pvp"][0])) . '<br><br>';
listData($tadminlist["pvp"]);

echo "<br>SORTED ASC<br>";
sort($tadminlist["pvp"]);
listData($tadminlist["pvp"]);

echo "<br>SORTED DESC<br>";
rsort($tadminlist["pvp"]);
listData($tadminlist["pvp"]);


function listData($arr) {
    foreach ($arr as $tlist) echo join(', ', $tlist) . '<br>';
}

OUTPUTS

line = name, type, beta, final, modded, classification, sf, tod, weather, es2

mapname1, pvp, y, n, , land, n, dawn, fog, y
mapname3, pvp, yy, n, y, air, y, day, rain, n
mapname2, pvp, n, y, n, sea, n, night, clear, n

SORTED ASC
mapname1, pvp, y, n, , land, n, dawn, fog, y
mapname2, pvp, n, y, n, sea, n, night, clear, n
mapname3, pvp, yy, n, y, air, y, day, rain, n

SORTED DESC
mapname3, pvp, yy, n, y, air, y, day, rain, n
mapname2, pvp, n, y, n, sea, n, night, clear, n
mapname1, pvp, y, n, , land, n, dawn, fog, y

To sort by any other element would require usort, eg to sort by classification

usort($tadminlist["pvp"], function($a, $b) { return $a['classification'] <=> $b['classification']; } );

 

  • Like 1
  • Great Answer 1

Share this post


Link to post
Share on other sites

Grrrrrr.....
Just when I think I have it figured out you folks throw out such sweet, short and simple solutions!

:)

Thank you very much

I can't find where to mark this thread as solved, please mark it for me, thank you

Edited by dodgeitorelse3
mark as solved

Share this post


Link to post
Share on other sites

There is no "Solved" button anymore"

The people who revised the software for these forums, which are all about people looking for solutions to their problems, decided that a Solved option would not be required.

The closest now is the "Recommend" option.

Share this post


Link to post
Share on other sites
Just now, Barand said:

The closest now is the "Recommend" option.

...which isn't even available for regular users :( And it's not just a matter of making the link visible - it's tied into the moderation system.

The Like button is next best.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.