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 protected] - 23452524352
   6 - Cldo -  Surname  - [email protected] - 23452524352
   3 - Aldo -  Surname  - [email protected] - 23452524352

//Example sorted:
//Id - Name - Last name -       Email       - Phone Number
   3 - Aldo -  Surname  - [email protected] - 23452524352
   2 - Bldo -  Surname  - [email protected] - 23452524352
   6 - Cldo -  Surname  - [email protected] - 23452524352

Here is the array.

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

    [0] => Array ( 
        [0] => 2
        [1] => "Bldo" 
        [2] => "Surname" 
        [3] => "[email protected]"
        [4] => 3243125152 
    )

    [1] => Array ( 
        [0] => 6
        [1] => "Cldo" 
        [2] => "Surname" 
        [3] => "[email protected]"
        [4] => 3243125152 
    ) 

    [2] => Array ( 
        [0] => 3
        [1] => "Aldo" 
        [2] => "Surname" 
        [3] => "[email protected]"
        [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.

2

3 Answers 3

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 protected]",3243125152),
    array(6,"Cldo","Surname","[email protected]",3243125152),
    array(2,"Aldo","Surname","[email protected]",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.

Sign up to request clarification or add additional context in comments.

1 Comment

If you want you can avoid the first foreach and use only $names = array_column($contacts, '1');
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>

Comments

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; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.