Xeoncross Posted June 18, 2008 Share Posted June 18, 2008 I have an array that contains strings and other arrays. I need to know how to sort it so that the strings are at the top of the array and the arrays are at the bottom of the array. <?php $myarray = array( 0 => array( 'sub1' => array('value' => 'Sub One', 'comment' => 'This is sub1'), 'sub2' => 'Sub Two', ), 1 => 'Value 1', 2 => array( 'sub1' => array('value' => 'Sub One', 'comment' => 'This is sub1'), 'sub2' => 'Sub Two', ), 3 => 'Value 3' ); ///////////////into $myarray = array( 0 => 'Value 1', 1 => 'Value 3', 2 => array( 'sub1' => array('value' => 'Sub One', 'comment' => 'This is sub1'), 'sub2' => 'Sub Two', ), 3 => array( 'sub1' => array('value' => 'Sub One', 'comment' => 'This is sub1'), 'sub2' => 'Sub Two', ) ); ?> My guess is that usort() should be used somehow. I just don't want to have to cycle through each array piece and LOOK at it to check if it is an array or not... Link to comment https://forums.phpfreaks.com/topic/110801-how-to-sort-multidimensional-arrays-by-array-or-string-type/ Share on other sites More sharing options...
thebadbad Posted June 18, 2008 Share Posted June 18, 2008 sort() Link to comment https://forums.phpfreaks.com/topic/110801-how-to-sort-multidimensional-arrays-by-array-or-string-type/#findComment-568533 Share on other sites More sharing options...
thebadbad Posted June 18, 2008 Share Posted June 18, 2008 The manual has this to say though: Be careful when sorting arrays with mixed types values because sort() can produce unpredictable results. But for your example it works. Link to comment https://forums.phpfreaks.com/topic/110801-how-to-sort-multidimensional-arrays-by-array-or-string-type/#findComment-568537 Share on other sites More sharing options...
Xeoncross Posted June 18, 2008 Author Share Posted June 18, 2008 sort() uh... no. Look at my example code again. I stated that I want to sort by TYPE - as in STRING first then ARRAY. Not by highest-lowest like sort does... Link to comment https://forums.phpfreaks.com/topic/110801-how-to-sort-multidimensional-arrays-by-array-or-string-type/#findComment-568541 Share on other sites More sharing options...
thebadbad Posted June 18, 2008 Share Posted June 18, 2008 I actually read your post. <?php $myarray = array( 0 => array( 'sub1' => array('value' => 'Sub One', 'comment' => 'This is sub1'), 'sub2' => 'Sub Two', ), 1 => 'Value 1', 2 => array( 'sub1' => array('value' => 'Sub One', 'comment' => 'This is sub1'), 'sub2' => 'Sub Two', ), 3 => 'Value 3' ); sort($myarray); echo '<pre>', print_r($myarray, true), '</pre>'; ?> gives me Array ( [0] => Value 1 [1] => Value 3 [2] => Array ( [sub1] => Array ( [value] => Sub One [comment] => This is sub1 ) [sub2] => Sub Two ) [3] => Array ( [sub1] => Array ( [value] => Sub One [comment] => This is sub1 ) [sub2] => Sub Two ) ) ? On PHP 5.2.2 BTW. Link to comment https://forums.phpfreaks.com/topic/110801-how-to-sort-multidimensional-arrays-by-array-or-string-type/#findComment-568548 Share on other sites More sharing options...
Xeoncross Posted June 20, 2008 Author Share Posted June 20, 2008 I actually read your post. So you did Sorry, I forgot that sort deletes the keys and then sorts by value type. So you are correct. However, I wanted to keep the keys and sort by value type - but that is impossible now that I have thought about it Link to comment https://forums.phpfreaks.com/topic/110801-how-to-sort-multidimensional-arrays-by-array-or-string-type/#findComment-570325 Share on other sites More sharing options...
sasa Posted June 20, 2008 Share Posted June 20, 2008 use asort() function Link to comment https://forums.phpfreaks.com/topic/110801-how-to-sort-multidimensional-arrays-by-array-or-string-type/#findComment-570346 Share on other sites More sharing options...
thebadbad Posted June 20, 2008 Share Posted June 20, 2008 However, I wanted to keep the keys and sort by value type - but that is impossible now that I have thought about it Your example stated the opposite, but yeah, as sasa suggested, asort() will do the job Link to comment https://forums.phpfreaks.com/topic/110801-how-to-sort-multidimensional-arrays-by-array-or-string-type/#findComment-570391 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.