The14thGOD Posted June 10, 2011 Share Posted June 10, 2011 Is there a way to sort an array of objects by the objects variable name or a property within the object? I'm new to OOP so sorry if this is dumb. <?php //example code $members = array(); $member_name = 'John Doe'; $member_last_name = 'Doe'; $_GET['filter'] == 'alpha' ? $array_safe_name = str_replace(' ','_',$member_last_name) : $array_safe_name = str_replace(' ','_',$member_name); $array_safe_name = str_replace('.','',$array_safe_name); $array_safe_name = str_replace("'",'',$array_safe_name); ${$array_safe_name} = new njl_member($member_name,$member_title,$member_image,$member_phone,get_permalink()); array_push($members,${$array_safe_name}); foreach($members as $member){ //do stuff } ?> ksort/asort obviously wont work (as far as I know them....) So if I have 'John Doe', 'Joe Adams', 'Jane Smith', I'd like the foreach to display them Joe Adams John Doe Jane Smith Thanks for any and all help, Justin Quote Link to comment https://forums.phpfreaks.com/topic/239007-sorting-an-array-of-objects/ Share on other sites More sharing options...
The14thGOD Posted June 10, 2011 Author Share Posted June 10, 2011 solved with: http://php.net/manual/en/function.usort.php Quote Link to comment https://forums.phpfreaks.com/topic/239007-sorting-an-array-of-objects/#findComment-1228084 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.