TheBrandon Posted August 30, 2011 Share Posted August 30, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/246060-sort-an-array-on-a-specific-key/ Share on other sites More sharing options...
xyph Posted August 30, 2011 Share Posted August 30, 2011 <?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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/246060-sort-an-array-on-a-specific-key/#findComment-1263685 Share on other sites More sharing options...
TheBrandon Posted August 30, 2011 Author Share Posted August 30, 2011 http://www.php.net/manual/en/function.array-multisort.php Yeah I tried that but I don't think I could figure out the syntax. Is there anyway you could show me an example using the array syntax above? Quote Link to comment https://forums.phpfreaks.com/topic/246060-sort-an-array-on-a-specific-key/#findComment-1263686 Share on other sites More sharing options...
jcbones Posted August 31, 2011 Share Posted August 31, 2011 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/246060-sort-an-array-on-a-specific-key/#findComment-1263744 Share on other sites More sharing options...
TheBrandon Posted August 31, 2011 Author Share Posted August 31, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/246060-sort-an-array-on-a-specific-key/#findComment-1264019 Share on other sites More sharing options...
TheBrandon Posted September 15, 2011 Author Share Posted September 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246060-sort-an-array-on-a-specific-key/#findComment-1269672 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.