Jump to content

Sorting an array of objects


The14thGOD

Recommended Posts

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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