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... Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 18, 2008 Share Posted June 18, 2008 sort() Quote Link to comment 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. Quote Link to comment 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... Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
sasa Posted June 20, 2008 Share Posted June 20, 2008 use asort() function Quote Link to comment 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 Quote Link to comment 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.