1

I need to print this full multidimensional array in a table and sort it by name [A-Z].

//Example not sorted:
//Id - Name - Last name -      Email        - Phone number
   2 - Bldo -  Surname  - email@example.com - 23452524352
   6 - Cldo -  Surname  - email@example.com - 23452524352
   3 - Aldo -  Surname  - email@example.com - 23452524352

//Example sorted:
//Id - Name - Last name -       Email       - Phone Number
   3 - Aldo -  Surname  - email@example.com - 23452524352
   2 - Bldo -  Surname  - email@example.com - 23452524352
   6 - Cldo -  Surname  - email@example.com - 23452524352

Here is the array.

// Array with the contacts details
$contacts = array (

    [0] => Array ( 
        [0] => 2
        [1] => "Bldo" 
        [2] => "Surname" 
        [3] => "email@example.com"
        [4] => 3243125152 
    )

    [1] => Array ( 
        [0] => 6
        [1] => "Cldo" 
        [2] => "Surname" 
        [3] => "email@example.com"
        [4] => 3243125152 
    ) 

    [2] => Array ( 
        [0] => 3
        [1] => "Aldo" 
        [2] => "Surname" 
        [3] => "email@example.com"
        [4] => 3243125152 
    ) 
);

Here is the html.

<?php
    <!-- Resoults from db -->
    <table> 

        <thead>
            <tr>
                <th>Checkbox</th>
                <th>First name</th>
                <th>Last name</th>
                <th>Email</th>
                <th>Phone number</th>
                <th>Update</th>
            </tr>
        </thead>

        <tbody>

            <?php
            // Print contacts here
            ?>

        </tbody>

    </table>
?>

I tried only with one array and it works, but I can't figure it out how to make it with the multidimensional array.

Mukyuu
  • 6,436
  • 8
  • 40
  • 59
Nello796
  • 47
  • 5
  • Possible duplicate of [How to Sort Multi-dimensional Array by Value?](https://stackoverflow.com/questions/2699086/how-to-sort-multi-dimensional-array-by-value) – Umair Khan Nov 20 '19 at 12:05
  • You can use PHP function array_multisort. In the examples, you find enough suggestions how to use it. For example, have a look at https://www.php.net/manual/en/function.array-multisort.php#114076 – RWC Nov 20 '19 at 15:33

3 Answers3

1

Start by creating an array with values of what you want to sort by, in this case by name.

Then assign the values of $contacts to an array where the keys will be names and values will be the contacts values

Then you only need to sort the results and it's done.

$contacts = array (
    array(2,"Bldo","Surname","email@example.com",3243125152),
    array(6,"Cldo","Surname","email@example.com",3243125152),
    array(2,"Aldo","Surname","email@example.com",3243125152));

$names = array();
foreach($contacts as $contact) {
    array_push($names,$contact[1]);
}

$result = array();
foreach($names as $key => $val) {
    $result[$val] = $contacts[$key];
}

array_multisort($result);

print_r($result);

Try this.

RuiVBoas
  • 364
  • 4
  • 10
0

Create your own sorting function and apply it on the array.

Then

    <tbody>
        <?php
          foreach ($contact as &$value) {
            //do what you want with your contact ($value)
          }
        ?>
    </tbody>
0

Data sorting could be done with array_multisort and array_column functions Demo:

$keys = array_column($contacts, 1);
array_multisort($keys, SORT_ASC, $contacts);

In table you can use simple foreach. Put next between tbody tags:

<?php foreach($contacts as $record): ?>
 <tr>
    <?php foreach($record as $item): ?>
         <td><?= $item; ?></td> 
    <?php endforeach; ?>
         <td>..stuff for update button..</td>
 </tr>
<?php endforeach; ?>
Aksen P
  • 4,564
  • 3
  • 14
  • 27