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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化