PHP:按给定数组对递归数组进行排序

时间:2019-03-18 08:08:02

标签: php arrays sorting

我需要对数组进行排序

 //position (zero indexed) of required columns
 $columnsToProcess=[0,3,4]
 for ($c=0; $c < $num; $c++) {
 //do something with value if column's index is in the required columns array 
            if(in_array($c,$columnsToProcess)){
                //value at position $c equals $data[$c]
                echo $data[$c] . "<br />\n";
             }          
        }

通过此顺序参数:

$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')
        );

$order_setup= array( 'mailnickname', 'telephonenumberextension', 'sn', 'givenname', 'has_photo', 'workspace_workplace', 'company', 'department', 'group', 'position', 'supervisor_name', 'mail', 'mobile', 'telephonenumber', 'extensionattribute1', ); 需要按$tableColumns排序。

我尝试了类似的方法,但是效果不佳:

$order_setup

5 个答案:

答案 0 :(得分:3)

@evilReiko版本的扩展版本:

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

这里unset处理了$tableColumns的值,并在循环之后-将$tableColumns的其余部分合并到$newArray

答案 1 :(得分:2)

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

可能还有其他方法可以执行此操作,但是上面的代码可以完成此工作。

注意,它将不包括$order_setup中未包含的内容。

答案 2 :(得分:2)

您可能正在寻找array_intersect_key。这将通过第二个数组的键自动过滤和排序第一个数组的项目。 array_flip交换每个项目的键和值。

array_intersect_key($tableColumns, array_flip($order_setup))

结果:

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',
)

答案 3 :(得分: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);

答案 4 :(得分:0)

这样的事情怎么办?

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