Sorting a multi-dimentional array in PHP

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

I have made a multi dimentional array in PHP to store phone numbers of friends' and family. Trouble is, I want to sort the table using PHP because it would be too time consuming to sort it by hand heres the basic structure of the array.

<?php
$contacts_array
= array(
array(
last_name = \"\",
first_name = \"\",
area_code = \"\",
number = \"\"
),
array(
last_name = \"\",
first_name = \"\",
area_code = \"\",
number = \"\"
)
);
?>

I would like to sort it in alphabetacal order by the first name.

Thanks

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

http://www.php.net/manual/en/function.sort.php specifically http://www.php.net/manual/en/function.asort.php

I'm terrible at offering coding advice, so I'll just point you at php.net. Smiling

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

<?php
function sort_list($a, $b)
{
    if (
strtolower($a['first_namd']) == strtolower($b['first_namd'])) return 0;
    return (
strtolower($a['first_namd']) > strtolower($b['first_namd'])) ? -1 : 1;
}

$contacts = array(
        array(
           
'last_name' => \"Schmoe\",
            'first_name' => \"Joe\",
            'area_code' => \"90210\",
            'number' => \"555-5555\"
        ),
        array(
            'last_name' => \"Doe\",
            'first_name' => \"John\",
            'area_code' => \"12345\",
            'number' => \"555-555\"
        )
    );

echo \"<html><body><pre>\n\";
print_r(
$contacts);
echo \"\n\";
uasort(
$contacts, 'sort_list');
print_r(
$contacts);

echo \"</pre></body></html>\";
?>

working example: http://host.maxalbert.com/testing_center/array_sorting.php
source code: http://host.maxalbert.com/testing_center/phps_files/array_sorting.phps

PHP Docs: uasort()

Mark Hensler
If there is no answer on Google, then there is no question.

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Thanks for the code Mark but it's not working for me :S

I have this in data.php

<?php
//contacts array is here
 
 
$global_vars = array(\"_GET\", \"_POST\");
  foreach(
$global_vars as $global_var)
    {
     
$keys = array_keys($$global_var);
      foreach(
$keys as $key)
        {
          $
$key = trim(${$global_var}[$key]);
        }
    }

  function sort_list(
$a, $b)
        {
          if (strtolower(
$a['first_name']) == strtolower($b['first_name'])) return 0;
          return (strtolower(
$a['first_name']) > strtolower($b['first_name'])) ? -1 : 1;
        }

  uasort(
$contact_array, 'sort_list');
 
 
$max_contacts = count($contact_array);
?>

I have this in index.php

<?php
<html>
  <
head>
   
      include(\
"data.php\");
      for(
$i = 0; $i < $max_contacts; $i++)
              {
               
$print_contacts .= \"<tr><td>\" . ($i + 1) . \"</td>\";
               
$print_contacts .= \"<td>\" . $contact_array[$i][last_name] . \"</td>\";
               
$print_contacts .= \"<td>\" . $contact_array[$i][first_name] . \"</td>\";
               
$print_contacts .= \"<td>\" . $contact_array[$i][area_code] . \"</td>\";
               
$print_contacts .= \"<td>\" . $contact_array[$i][area_code] . \" \" . $contact_array[$i][number] . \"</td></tr>\n\";
              }
   
    <link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\" />
    <title>Contact Phone Numbers</title>
  </head>
  <body>
    <table cellspacing=\"0\" cellpadding=\"0\" class=\"table-contacts\">
      <tr>
        <th>&nbsp;</th>
        <th>Last Name</th>
        <th>First Name</th>
        <th>Area</th>
        <th>Phone Number</th>
      </tr>
     
        uasort(
$contact_array, 'sort_list');
        echo
$print_contacts;
     
    </table>
  </body>
</html>
?>

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'm a bit confused why your doing this:

<?php
  $global_vars
= array(\"_GET\", \"_POST\");
  foreach(
$global_vars as $global_var)
    {
     
$keys = array_keys($$global_var);
      foreach(
$keys as $key)
        {
          $
$key = trim(${$global_var}[$key]);
        }
    }
?>
Try this instead:
<?php
extract
($_GET);
extract($_POST);
?>
The uasort() in index.php isn't necessary. The one in data.php should be doing the job.

The problem here is that uasort() maintains index association. If you don't want this, you can substitute with usort(). You can still use uasort() and avoid this problem by using a foreach() loop() rather than a for() loop.

Try this for index.php:

<?php
<html>
  <
head>
    <
link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\" />
    <title>Contact Phone Numbers</title>
  </head>
  <body>
    <table cellspacing=\"0\" cellpadding=\"0\" class=\"table-contacts\">
      <tr>
        <th>&nbsp;</th>
        <th>Last Name</th>
        <th>First Name</th>
        <th>Area</th>
        <th>Phone Number</th>
      </tr>

include(\"data.php\");
foreach (
$contact_array as $key=>$contact) {
    echo \"<tr><td>\" . (
$key) . \"</td>\";
    echo \"<td>\" .
$contact[last_name] . \"</td>\";
    echo \"<td>\" .
$contact[first_name] . \"</td>\";
    echo \"<td>\" .
$contact[area_code] . \"</td>\";
    echo \"<td>\" .
$contact[number] . \"</td></tr>\n\";
}

    </table>
  </body>
</html>
?>

Mark Hensler
If there is no answer on Google, then there is no question.

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Quote: Originally posted by Mark Hensler
I'm a bit confused why your doing this:

<?php
  $global_vars
= array(\"_GET\", \"_POST\");
  foreach(
$global_vars as $global_var)
    {
     
$keys = array_keys($$global_var);
      foreach(
$keys as $key)
        {
          $
$key = trim(${$global_var}[$key]);
        }
    }
?>

I have that because I have register globals turned off as compared to when I used to have it on, so instead of changeing every one of my pages to work with register globals off I just add that in Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.