danthemilk Posted July 6, 2009 Share Posted July 6, 2009 $staffArray = array(); $result = mysql_query("SELECT * FROM staff ORDER BY SUBSTRING_INDEX(name, ' ', -1) ASC"); while($row = mysql_fetch_assoc($result)) { array_push($staffArray, array( $row['name'] , $row['title'] , $row['office'] , $row['bio'] , $row['contact'] , $row['imageURL'] )); } for($i = 0; $i < sizeof($staffArray); $i++) { if($staffArray[$i][0] == 'List Topper') { // how can I move this element in the array to $staffArray[0] } } any help with this would be appreciated. It seems like it should be simple but I'm having a hard time with it. I just need to check for a certain name and then move it to the first position in the array. Link to comment https://forums.phpfreaks.com/topic/164980-reposition-array-element/ Share on other sites More sharing options...
rhodesa Posted July 6, 2009 Share Posted July 6, 2009 try: $staffArray = array(); $result = mysql_query("SELECT * FROM staff ORDER BY SUBSTRING_INDEX(name, ' ', -1) ASC"); while($row = mysql_fetch_assoc($result)) { array_push($staffArray, array( $row['name'] , $row['title'] , $row['office'] , $row['bio'] , $row['contact'] , $row['imageURL'] )); } for($i = 0; $i < sizeof($staffArray); $i++) { if($staffArray[$i][0] == 'List Topper') { list($row) = array_slice($staffArray,$i,1); array_unshift($staffArray,$row); } } Link to comment https://forums.phpfreaks.com/topic/164980-reposition-array-element/#findComment-869948 Share on other sites More sharing options...
p2grace Posted July 6, 2009 Share Posted July 6, 2009 Yeah array_unshift is the way to go... here is the php.net link. http://us.php.net/manual/en/function.array-unshift.php Link to comment https://forums.phpfreaks.com/topic/164980-reposition-array-element/#findComment-869951 Share on other sites More sharing options...
danthemilk Posted July 6, 2009 Author Share Posted July 6, 2009 Thanks a ton fellas. I ended up doing it like this, because your solution seemed to cause an infinite loop or something: $result = mysql_query("SELECT * FROM staff ORDER BY SUBSTRING_INDEX(name, ' ', -1) ASC"); while($row = mysql_fetch_assoc($result)) { array_push($staffArray, array( $row['name'] , $row['title'] , $row['office'] , $row['bio'] , $row['contact'] , $row['imageURL'] )); } for($i = 0; $i < sizeof($staffArray); $i++) { if($staffArray[$i][0] == 'Top of List') { $shiftIndex = $i; } } list($row) = array_slice($staffArray,$shiftIndex,1); array_unshift($staffArray,$row); Link to comment https://forums.phpfreaks.com/topic/164980-reposition-array-element/#findComment-869972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.