0

I need to sort array:

$tableColumns=array(
            'icons'                         => '',
            'mailnickname'                  => t('Internal code'),
            'telephonenumberextension'      => t('Internal extension'),
            'sn'                            => t('Last name'),
            'givenname'                     => t('First name'),
            'cn'                            => t('Username'),
            'level'                         => t('Role'),
            'has_photo'                     => t('Photo'),
            'workspace_workplace'           => t('Workspace'),
            'workspace_layout'              => t('Officelayout'),
            'company'                       => t('Company'),
            'department'                    => t('Department'),
            'group'                         => t('Group'),
            'position'                      => t('Position'),
            'supervisor_name'               => t('Supervisor'),
            'description'                   => t('Description'),
            'mail'                          => t('Email'),
            'mobile'                        => t('Mobile'),
            'telephonenumber'               => t('Telephone'),
            'extensionattribute1'           => t('City'),
            'updated'                       => t('Synchronized')
        );

by this order params:

$order_setup= array(
            'mailnickname',
            'telephonenumberextension',
            'sn',
            'givenname',
            'has_photo',
            'workspace_workplace',
            'company',
            'department',
            'group',
            'position',
            'supervisor_name',
            'mail',
            'mobile',
            'telephonenumber',
            'extensionattribute1',
        );

$tableColumns need to be sorted by $order_setup.

I try something like this but not work well:

$array_order = array_flip($tableColumns);

        usort($array_order, function ($a, $b) use ($order_setup) {
            $pos_a = array_search($a, $order_setup);
            $pos_b = array_search($b, $order_setup);
            return $pos_a - $pos_b;
        });

        $tableColumns_sorted = array();

        foreach($array_order as $o)
        {
            $tableColumns_sorted[$o]=$tableColumns[$o];
        }

        echo '<pre>', var_dump($tableColumns_sorted), '</pre>';
2
  • 2
    And columns not in $order_setup? Commented Mar 18, 2019 at 8:14
  • There's no "recursive" (nested) array in your code. Did you mean associative? Commented Mar 18, 2019 at 8:44

5 Answers 5

3

Extended version of @evilReiko's version:

$newArray = [];
foreach ($order_setup as $field) {
    $newArray[$field] = $tableColumns[$field];
    unset($tableColumns[$field]);
}
$newArray = array_merge($newArray, $tableColumns);

Here you unset processed $tableColumns values and after the loop - merge rest of $tableColumns to $newArray.

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

2 Comments

This will include ones not in $order_setup. Good :)
There is a lot of great answers but this is the best one. I can't belive that I'm after 15 years in PHP stack in this simple problem and solution. Thanks guys.
2

You might be looking for array_intersect_key. This automatically filters and orders the items of the first array by the keys of the second one. array_flip swaps the key and value of each item.

array_intersect_key($tableColumns, array_flip($order_setup))

Result:

array (
  'mailnickname' => 'Internal code',
  'telephonenumberextension' => 'Internal extension',
  'sn' => 'Last name',
  'givenname' => 'First name',
  'has_photo' => 'Photo',
  'workspace_workplace' => 'Workspace',
  'company' => 'Company',
  'department' => 'Department',
  'group' => 'Group',
  'position' => 'Position',
  'supervisor_name' => 'Supervisor',
  'mail' => 'Email',
  'mobile' => 'Mobile',
  'telephonenumber' => 'Telephone',
  'extensionattribute1' => 'City',
)

Comments

2
$tableColumnsSorted = [];
foreach($order_setup as $v) {
    $tableColumnsSorted[$v] = $tableColumns[$v];
}

There might be other ways to do this, but the above code does the job.

Notice, it will not include ones not in $order_setup.

1 Comment

Excatly what i had in mind, no need to make it complex
0

What about something like this?

    foreach($table_columns as $key => $value) {
        if(in_array($key, $order_setup)){
            $ordered_columns[$key] = $value;
        }
        else{
            $ordered_empty[$key] = null;
        }
    }

    print_r(array_merge($ordered_columns,$ordered_empty));

Comments

0
$array_order = array_merge(array_flip($order_setup), $tableColumns);
//Or you can do this way
$array_order = array_replace(array_flip($order_setup), $tableColumns);

2 Comments

Never seen array_flip with two arguments, what does that do? The manual only states one input.
Although this code might (or might not) solve the problem, a good answer should be accompanied by an explanation.

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.