Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:40:46+00:00 2026-06-14T13:40:46+00:00

I have a problem with sorting of an array. $infoGroup is the result of

  • 0

I have a problem with sorting of an array.
$infoGroup is the result of a ‘ldap_get_entries’ call earlier. As I step through this array I put the result in the array $names.
Then I want to sort $names in alfabetical order, I have tried a number of different methods but to no avail. The array always stays in the same order it was constructed.

What have I missed?

foreach($infoGroup[$i]['member'] as $member) {
 //echo "<li>".$member;
 $go = stripos($member, "n");
 unset($names);
 $ai++;
 if ( $go == 1 ) {
  // extract member name from string
  $temp = substr($member, 0, stripos($member, ","));
  // Strip the CN= and change to lowercase for easy handling
  $temp = str_replace("cn=", "", $temp);                                
  $names[$ai] = ($temp);
 }
 if (natsort($names)){
  foreach ($names as $key => $val) {
   echo "<li>";
   echo "$key $val";
  }
 }
}
$ai = 0;

This is the result however I try to sort the $names array:

  1. Henrik Lindbom
  2. Klaus Rödel
  3. Admin
  4. Bernd Brandstetter
  5. proxyuser
  6. Patrik Löfström
  7. Andreas Galic
  8. Martin Stalder
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:40:47+00:00Added an answer on June 14, 2026 at 1:40 pm

    Hmmm.. a bit hard to explain, but the issue is because you are sorting your array inside that foreach() loop. Essentially, since you are creating the array element in the iteration of the first loop, the natsort() only has 1 element to sort and your nested foreach() loop is only outputting that 1 element, which is then unset() at the second and further iterations…

    Extract that second foreach() that sorts and outputs and remove the unset() from the top of the first loop. This should output your desired results.

    Something like this…

    foreach($infoGroup[$i]['member'] as $member) {
     //echo "<li>".$member;
     $go = stripos($member, "n");
     $ai++;
     if ( $go == 1 ) {
      // extract member name from string
      $temp = substr($member, 0, stripos($member, ","));
      // Strip the CN= and change to lowercase for easy handling
      $temp = str_replace("cn=", "", $temp);                                
      $names[$ai] = ($temp);
     }
    }
    
     if (natsort($names)){
      foreach ($names as $key => $val) {
       echo "<li>";
       echo "$key $val";
      }
     }
    $ai = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very strange array sorting related problem in PHP that is driving
I got problem for sorting array of nsdate. so i have array of date
Im stuck on a sorting problem, I have an array with 10 numbers (1-10)
I have problem in sorting an array in ascending form and i don't know
I have an integer array sorting program here but I have a problem: whenever
I have a little problem using jQuery and sorting a JSON array. What I'm
I have a problem with sorting... I need to sort NSArray containing NSDictionaries .
I have a problem with sorting NSTableColumn contents. In my NSTableView there are three
I have a problem with sorting items in table in PHP. Here is what
I have a complex sorting problem with my SQL statement. I have a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.