CSmith1128 Posted July 17, 2007 Share Posted July 17, 2007 Ok.. i need some help sorting arrays..I have 3 arrays that are all linked together.. The first array is the user's id.. the second array is the users first name.. and the third is the users last nameso here are the arrays.user_id_array, user_fname_array, user_lname_arrayi want to sort the arrays by last name from A to Z.when i sort the last name, i want the first name array element and the user id array element to change with it.i want them to change together because each element in all three arrays all have to do with the same person. so element 0 in all three arrays will deal with one person, and element 1 in all three arrays will deal with the next person, and so on.. so they have to remain the same element ids for each array.. so if you change 1 element array, you have to change the other 2 with it..can anyone help me get started?thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted July 17, 2007 Share Posted July 17, 2007 It's not very efficient to use 3 arrays to store information about 1 person. Just use one array with nested arrays to hold the different information. <?php$people = array();$person = array();$person['id'] = 1;$person['fname'] = "Fred";$person['lname'] = "Smith";$people[] = $person;$person = array();$person['id'] = 2;$person['fname'] = "Bob";$person['lname'] = "Jones";$people[] = $person;print_r($people);foreach ($people as $person){ echo "id: " . $person['id'] . "\n"; echo "fname: " . $person['fname'] . "\n"; echo "lname: " . $person['lname'] . "\n";}?> Once you do that, you can use the usort function to sort by whichever element you want.http://www.php.net/manual/en/function.usort.php Link to comment Share on other sites More sharing options...
CSmith1128 Posted July 18, 2007 Author Share Posted July 18, 2007 hmmm.. what makes it less efficient?i like it though.how would i delete a "person" from my "people" array?so, if($id > 20) { // delete that single element from array } Link to comment Share on other sites More sharing options...
justsomeguy Posted July 18, 2007 Share Posted July 18, 2007 You would need to scan through the array and find the item to delete. You can use a function to delete a certain id. function delete_person($id){ global $people; foreach ($people as $key => $person) { if ($person['id'] == $id) { unset($people[$key]); break; } }}delete_person(7); If you want to delete every person with an id greater then a certain number, you can use array_filter. function filter_people($p){ global $max_id; if ($p['id'] > $max_id) return false; return true;}$max_id = 20;array_filter($people, "filter_people"); Link to comment Share on other sites More sharing options...
Skemcin Posted July 18, 2007 Share Posted July 18, 2007 Is there not a function in PHP to convert an array into a query structure that allows you to query using standard SQL? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 18, 2007 Share Posted July 18, 2007 Not that I'm aware of. Link to comment Share on other sites More sharing options...
Skemcin Posted July 18, 2007 Share Posted July 18, 2007 Not that I'm aware of.Interesting to know, I recently ran into a similar issue with structures nested in arrays and worked through it pretty easily with a function like I describe.I know this is not PHP code, but it might be of some interest as it relates to the issue:<cfset var q = queryNew("mykey,type,title,delivery,quantity")><cfloop index="i" from="1" to="#ArrayLen(session.brochure)#"><cfset QueryAddRow(q) /><cfset q["mykey"][i]=session.brochure[i].key /><cfset q["type"][i]=session.brochure[i].type /><cfset q["title"][i]=session.brochure[i].title /><cfset q["delivery"][i]=session.brochure[i].delivery /><cfset q["quantity"][i]=session.brochure[i].quantity /></cfloop><cfquery name="r" dbtype="query" result="request.RequestTime"> SELECT * FROM q WHERE mykey = '#arguments.key#'</cfquery> There is a little more happening before and after this code, but this is what is used to convert an array with nested structures into an object that can be queried.Anyway, just interesting to learn how some of the languages differ. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 18, 2007 Share Posted July 18, 2007 You were just waiting to bust that out weren't you Link to comment Share on other sites More sharing options...
Skemcin Posted July 18, 2007 Share Posted July 18, 2007 You were just waiting to bust that out weren't you lmfao - err, uhhh, **cough** - ok, ok I admit - you caught me with my hand in the cookie jar. Link to comment Share on other sites More sharing options...
jhecht Posted July 19, 2007 Share Posted July 19, 2007 You would need to scan through the array and find the item to delete. You can use a function to delete a certain id.function delete_person($id) { global $people; foreach ($people as $key => $person) { if ($person['id'] == $id) { unset($people[$key]); break; } } } delete_person(7); If you want to delete every person with an id greater then a certain number, you can use array_filter. function filter_people($p) { global $max_id; if ($p['id'] > $max_id) return false; return true; } $max_id = 20; array_filter($people, "filter_people"); My solution to this one would be to just use 1 function to delete people from the multi-dimensional array(as stated, using 3 different arrays is a bit pointless and repetative). I believe something like this would work; <?php function delete_person($people,$id){ //This function takes two arguments, the first being the array, second being the Id(s) you want deteted; foreach($people as $key=>$person){ //Looping through the array, grabbing the iteration number($key) and the array of values. if(@is_array($id)){ if(in_array($person['id'],$id)){ //If the person's id is in the array of Id's to be deleted, delete it; //If not, do nothing to this iteration and continue; unset($people[$key]); //Voila, it is done; } }else{ //If $id is not an array, it must be an integer; if(is_numeric($id)){ //this is just a test, to make sure some form of hacking doesn't go on later; if($person['id'] == $id){ unset($people[$key]); //Unset done; break; //Break in this case as there is only one item to be deleted; } }else{ //Hacking attempt; return false; } } } return true; } ?> This function could be altered to accept a third, optional argument which entails which mode to use(i.e. setting the mode to something like "gt" would delete all iterations of the array greater than $id, setting it to "lt" would delete all iterations less that $id, and in these modes, arrays would not be allowed, so an additional check would have to be done).Thats my personal solution to this. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.