Jump to content

Sort an array on a specific key?


TheBrandon

Recommended Posts

Hello all,

 

I have a rather large array and I need to list it's contents in an alphabetical list.

 

The array is built like this:

 

$array['query']['row']['0']['title'] = 'Bob';

$array['query']['row']['1']['title'] = 'Andy';

$array['query']['row']['2']['title'] = 'Tom';

 

I need to sort it so it is in alphabetical order based on the title. Is there an easy way to do this?

Link to comment
https://forums.phpfreaks.com/topic/246060-sort-an-array-on-a-specific-key/
Share on other sites

<?php

$array['a']['b'][0] = array( 'name'=>'Charlie','position'=>'dev' );
$array['a']['b'][1] = array( 'name'=>'Tom','position'=>'admin' );
$array['a']['b'][2] = array( 'name'=>'Adam','position'=>'dev' );
$array['a']['b'][3] = array( 'name'=>'Peter','position'=>'web' );

// Show the default array
echo '<pre>';
print_r( $array );
echo "\n";

// Build a list of values to sort by
foreach( $array['a']['b'] as $key => $row )
$name[$key] = $row['name'];
// Perform the sort, now that we have a duplicated 1-D array to match
array_multisort( $name, SORT_ASC, $array['a']['b'] );

// Show the modified array
print_r( $array );
echo '</pre>';

?>

 

To sort by multiple sub-arrays, check out PHP's example here

<?php
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);

// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>

Sure:

<?php
$array['query']['row']['0']['title'] = 'Bob';
$array['query']['row']['1']['title'] = 'Andy';
$array['query']['row']['2']['title'] = 'Tom';


foreach($array['query']['row'] as $k=>$v) {
$sort[] = $k['title'];
}

array_multisort($sort,SORT_ASC,$array['query']['row']);

echo '<pre>' . print_r($array,true) . '</pre>';
?>

Thank you for the help. I'm having some trouble implementing it with my code. Can you please let me know where I'm going wrong?

 

Do I need to modify my output somehow? It seems right to me as long as we are overwriting the $array array with the sorted version.

 

	//get the xml id from the url
$xml_id = $_GET['xml_id'];

//get the actual xml filename using our $_XML array assigned in config
$xml_filename = $_XML[$xml_id];

//convert the xml to an array
$array =  xml2array(file_get_contents(''.$_CONFIG['site_url'].'xml/'.$xml_filename.''));

foreach($array['query']['row'] as $k=>$v) {
	$sort[] = $k['title'];
}

array_multisort($sort, SORT_ASC, $array['query']['row']);

//echo '<pre>' . print_r($array,true) . '</pre>';

switch ($XML_File){
	case "1":

			$sub_type = $accomodations_subcat[$_GET['cat_id']];

			echo '<ul>';

			foreach ($array['query']['row'] as $key => $value) {

				if($value['subtype'] == $sub_type && $value['active'] == 1){

					echo '<li><a href="index.php?xml_id='.$_GET['xml_id'].'&cat_id='.$_GET['cat_id'].'&listing_id='.$key.'">'.$value['title'].'</a></li>';

				}

			}
echo '</ul>';

		break;

 

  • 3 weeks later...

Sure:

<?php
$array['query']['row']['0']['title'] = 'Bob';
$array['query']['row']['1']['title'] = 'Andy';
$array['query']['row']['2']['title'] = 'Tom';


foreach($array['query']['row'] as $k=>$v) {
$sort[] = $k['title'];
}

array_multisort($sort,SORT_ASC,$array['query']['row']);

echo '<pre>' . print_r($array,true) . '</pre>';
?>

 

I got it working. Thank you very much, everyone. The problem was the $k['title'] in this example. Needs to be $v['title'].

 

Works beautifully. Thanks again everyone.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.