1

我已经从长期的组合查询和文件读取过程中生成了三个字符串,每个字符串都相互关联。


例子:

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

我想根据版本号按升序对它们进行排序。

示例结果:

$versions = "1 2 3 4 5 6 7 8 9 10";
$weights = "50.2 60.5 70 10 35 60 60.1 50 75 15.98";
$ids = "512 318 943 326 112 191 961 977 441 155";

我的问题是:有没有比我现在做的更有效的方法?
请注意,这些字符串可能会变得很大,到目前为止我见过的最大的是大约 600 个不同的版本

我执行以下操作:

  • 爆炸字符串
  • 复制以版本号为键的数组
  • 按键排序数组
  • 内爆字符串

这是代码和一个活生生的例子

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

$a_versions = explode(" ", $versions);
$a_weights = explode(" ", $weights);
$a_ids = explode(" ", $ids);

$s_versions = array();
$s_weights = array();
$s_ids = array();

//set keys to correspond to version number
foreach($a_versions as $key => $ver){
    $s_versions[$ver] = $a_versions[$key];
    $s_weights[$ver] = $a_weights[$key];
    $s_ids[$ver] = $a_ids[$key];
}

//sort according to keys
ksort($s_versions, SORT_NUMERIC);
ksort($s_weights, SORT_NUMERIC);
ksort($s_ids, SORT_NUMERIC);

//implode back
$versions = implode(" ", $s_versions); 
$weights = implode(" ", $s_weights); 
$ids = implode(" ", $s_ids); 

echo "
    <pre>
        $versions
        $weights
        $ids
    </pre>
";

/*==========
Results
    1 2 3 4 5 6 7 8 9 10
    50.2 60.5 70 10 35 60 60.1 50 75 15.98
    512 318 943 326 112 191 961 977 441 155
==========*/

性能提升#1:

用 array_combine 替换 foreach 循环可能会带来一点性能提升。– 斯文斯

确实如此,根据一个简单的单元测试,它快了大约 11-15%。

4

2 回答 2

1

This is the best I could find:

$versions = array_flip(explode(' ', $versions));
$weights = explode(' ', $weights);
$ids = explode(' ', $ids);

ksort($versions, SORT_NUMERIC);
foreach ($versions as $version => $idx) {
  $result[0][] = $version;
  $result[1][] = $weights[$idx];
  $result[2][] = $ids[$idx];
}

return array(
  implode(' ', $result[0]),
  implode(' ', $result[1]),
  implode(' ', $result[2]),
);

See: http://codepad.viper-7.com/flcvnO for a comparison of what I tested (including you initial code and the one with array_combine)

于 2012-05-22T11:36:25.023 回答
1

这是另一种方法。这是最简单的例子,没有键:

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

$a_versions = explode(" ", $versions);
$a_weights = explode(" ", $weights);
$a_ids =  explode(" ", $ids);

$version_weight_id = array_map(null, $a_versions, $a_weights, $a_ids);

print_r($version_weight_id);

如果您想要更多键控,则必须具有 map 功能,如下所示:

function version_weight_id($v, $w, $id) {

    return array('version' => $v, 'weight' => $w, 'id' => $id);

}

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

$a_versions = explode(" ", $versions);
$a_weights = explode(" ", $weights);
$a_ids =  explode(" ", $ids);

$version_weight_id = array_map('version_weight_id', $a_versions, $a_weights, $a_ids);

array_multisort($version_weight_id, $a_versions);

print_r($version_weight_id);

编辑:

这是另一种不需要地图功能的方法:

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

$a_versions = explode(" ", $versions);
$a_weights = explode(" ", $weights);
$a_ids =  explode(" ", $ids);

$weights_ids = array_map(null, $a_weights, $a_ids);
$versions_weights_ids = array_combine($a_versions, $weights_ids);

print_r($versions_weights_ids);

问题是你必须知道主键是版本,子数组 0 的键是权重,子数组 1 的键是 id。

要打印出您的结果,您将使用:

foreach($versions_weights_ids as $version => $weight_id) {
echo "
    <pre>
        $version
        {$weight_id[0]}
        {$weight_id[1]}
    </pre> ";
}

如果您正在寻找 ajax 解决方案,我会考虑 JSON,并使用类似的东西:

$versions = "1 2 5 4 10 6 8 7 3 9";
$weights = "50.2 60.5 35 10 15.98 60 50 60.1 70 75";
$ids = "512 318 112 326 155 191 977 961 943 441";

$a_versions = explode(" ", $versions);
$a_weights = explode(" ", $weights);
$a_ids =  explode(" ", $ids);

$weights_ids = array_map(null, $a_weights, $a_ids);
$versions_weights_ids = array_combine($a_versions, $weights_ids);

echo json_encode($versions_weights_ids);

这样,您可以使用框架的 each 方法或针对特定版本(或多个版本)进行输出。

于 2012-05-22T12:46:31.400 回答