dth981485742 2016-03-14 07:38
浏览 74
已采纳

如果存在相同的数字数组键,则添加一个增量

I have a following array and i need to sort this array based upon the key value. I wrote a code and got perfect result if all numeric keys are different.

$myarray_need_to_sort =Array
(
    [13] => 214
    [14] => 215
    [15] => 216
    [21] => 223
)

Here is my code

$order = array();
foreach ($myarray_need_to_sort as $value){
    $cat = Mage::getModel('catalog/category')->load($value);
    $order[$cat->getTabOrder()] = $value;
}   
ksort($order);
$sorted_order = array();
foreach ($order as $key=>$value)
{
    $sorted_order[$key] = $value;
}

print_r($sorted_order);

$cat->getTabOrder() is the sort order i am getting from backend and result is perfect

Array
(
    [1] => 216
    [2] => 223
    [3] => 215
    [4] => 214
)

The code is sorting array perfectly but the issue is that if same key exists ie someone added $cat->getTabOrder() same say 2 and 2 for two values then it is removing one key. I need to add one increment for this like

Array
(
    [1] => 216
    [2] => 223
    [2] => 215 -- it should be 3 and 4 should be 5 but it is removing 2 from here
    [4] => 214 -- should be 5
)
  • 写回答

1条回答 默认 最新

  • duanbei1903 2016-03-14 11:22
    关注

    As I said in a comment, this looks like an XY problem. You don't actually need to increment the keys, you need to keep all the values (including when the keys are duplicate) and sort them by key.

    I'll assume when two or more items have the same key, the order they come from the database must be kept in the final list.

    The goals are:

    • keep all the values;
    • sort them by keys;
    • when two or more items have the same key, keep the order they arrived.

    Having this in mind, a possible solution is to keep in $order, for each key the list of items that have that key. Sort the list by keys then either iterate over the array using two nested foreach loops or flatten it before using it.

    The code:

    // $order[$key] is the list of $cat items having $cat->getTabOrder() == $key
    $order = array();
    foreach ($myarray_need_to_sort as $value){
        $cat = Mage::getModel('catalog/category')->load($value);
        $key = $cat->getTabOrder();
        if (! array_key_exists($key, $order)) {
            // This is the first $cat having $key as tab order
            // Create a new list of $key
            $order[$key] = array();
        }
        // Add $value to the list of is $key (tab order)
        $order[$key][] = $value;
    }
    // Sort the outer array by keys
    ksort($order);
    

    A print_r($order) should display something like:

    Array
    (
        [1] => Array
            (
                [0] => 216
            )
        [2] => Array
            (
                [0] => 223
                [1] => 215
            )
        [4] => Array
            (
                [0] => 214
            )
    )
    

    Now, you can retrieve the values from the array by iterating over each sub-array:

    foreach ($order as $key => $list) {
        foreach ($list as $value) {
            echo('$key='.$key.': $value='.$value."
    ");
        }
    }
    

    It displays:

    $key=1: $value=216
    $key=2: $value=223
    $key=2: $value=215
    $key=4: $value=214
    

    If you don't care about the keys anymore and all you need is the list of $value after sorting then you can put the values into a new list in the double foreach above (instead of displaying them).

    Or you can write it in a more compact way using array_reduce() and array_merge():

    $new = array_reduce($order, 'array_merge', array());
    print_r($new);
    

    The output is:

    Array
    (
        [0] => 216
        [1] => 223
        [2] => 215
        [3] => 214
    )
    

    Notice that the original keys are lost, the new list is indexed sequentially.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题