简体   繁体   English

PHP-排序时修改数组

[英]PHP - Modify array while sorting

I need to sort an array of strings alphabetically and set each key to be the same as the value (because isset() is so much faster than in_array()). 我需要按字母顺序对字符串数组进行排序,并将每个键设置为与值相同(因为isset()比in_array()快得多)。 I can easily do this by making two passes over the array, but I was curious if it's possible to do it in only one pass. 我可以通过在数组上进行两次传递来轻松地做到这一点,但是我很好奇是否有可能仅通过一次传递。

I'd like to turn this: 我想转这个:

array(
    0 => 'car',
    1 => 'apple',
    2 => 'dog',
    3 => 'box'
)

...into this: ...变成这样:

array(
    'apple' => 'apple',
    'box' => 'box',
    'car' => 'car',
    'dog' => 'dog'
)

I've done some searches, but I can't seem to find anything about this (mostly I can only find stuff about maintaining the key-value relationship when sorting). 我已经进行了一些搜索,但似乎找不到任何相关信息(大多数情况下,我只能找到有关在排序时保持键值关系的信息)。

I'm aware this may be seen as an attempt at micro-optimization, but I'm asking more out of curiosity than desire to improve performance. 我知道这可能是对微优化的尝试,但出于好奇心,我要求的不仅仅是提高性能。 I'm hoping I can learn some useful stuff by tackling this problem so that I have the knowledge in the future. 我希望我可以通过解决此问题来学习一些有用的知识,以便将来有知识。

Edit: This is being used to alter an enum field in a mysql database. 编辑:这用于更改mysql数据库中的枚举字段。 When new features are added to the system, new enum values are needed in certain columns. 将新功能添加到系统后,某些列中需要新的枚举值。 Instead of tracking down errors and manually adding the new values, I'd like to make the system self-correcting. 我希望使系统自我更正,而不是跟踪错误并手动添加新值。

Basically, try to insert a row. 基本上,尝试插入一行。 If it fails with the truncation error that's thrown when an undefined enum value is added, then pull the enums for the column, check if the enum is present (it may be a different problem causing the error), if it isn't, and the value isn't NULL or empty string, add it. 如果由于添加未定义的枚举值而引发的截断错误而失败,则拉出该列的枚举,检查是否存在该枚举(可能是导致错误的另一个问题),如果不存在,并且该值不是NULL或为空字符串,请将其添加。

I'd like to keep the enum values in alphabetical order for my own sanity, and I was setting the keys and values to be the same so I could do two things: 为了我自己的理智,我想按字母顺序保留枚举值,并且我将键和值设置为相同,因此我可以做两件事:

  1. Do isset($values($new_value)) rather than in_array($value,$values). 执行isset($ values($ new_value))而不是in_array($ value,$ values)。 This is why I need the values in the keys. 这就是为什么我需要键中的值。

  2. Use implode to build the ALTER statement: $query_text = "ALTER TABLE table CHANGE column column ENUM('".implode("','",$values)."')...;"; 使用implode构建ALTER语句: $query_text = "ALTER TABLE table CHANGE column column ENUM('".implode("','",$values)."')...;";

I realize I could use implode("','",array_keys($values)) instead in this situation, but I'm curious about how I would do what I originally asked because there may come a time in the future where I need to do something more complex to an array while simultaneously sorting it. 我意识到在这种情况下我可以使用implode("','",array_keys($values))代替,但是我很好奇我会怎么做我最初要求的,因为将来可能会需要在对数组进行排序的同时对数组进行更复杂的处理。

TL;DR: I'm not interested in whether I should alter this array while sorting it, but how I would alter an array while sorting it. TL; DR:我不感兴趣,而排序它,我是否应该改变这个数组,但在排序它怎么改变一个数组。

Here is what you need: 这是您需要的:

<?php

$original = array(
    0 => 'car',
    1 => 'apple',
    2 => 'dog',
    3 => 'box'
);

$values = array_values($original);
sort($values);
$keys = array_combine($values, $values);

print_r($keys);
?>

and it print out 然后打印出来

Array
(
    [apple] => apple
    [box] => box
    [car] => car
    [dog] => dog
)

If the goal is to remove duplicates, sort alphabetically, and enjoy the performance of isset() , then just call array_flip($array) and then ksort($array) . 如果目标是删除重复项,按字母顺序排序并享受isset()的性能,则只需依次调用array_flip($array)ksort($array)

Specificlaly, array_flip() will weed out any duplicates and set up isset() 具体来说, array_flip()将清除所有重复项并设置isset()

Alternatively if you want to generate identical key-value pairs: 或者,如果要生成相同的键值对,请执行以下操作:

Code: ( Demo ) 代码:( 演示

$array = ['car', 'apple', 'dog', 'box'];
sort($array);
var_export(array_combine($array, $array));  // no need to call array_values()

Output: 输出:

array (
  'apple' => 'apple',
  'box' => 'box',
  'car' => 'car',
  'dog' => 'dog',
)

I'll just make a note to future researchers... Although isset() is faster than in_array() , the performance benefit is eliminated if you are going to greater trouble to reconfigure your array of data. 我只是给未来的研究人员做个记录...尽管isset()in_array()更快,但是如果您要重新配置数据数组时遇到更大的麻烦,则可以消除性能优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM