dshp9580656 2013-07-11 10:42
浏览 89
已采纳

在for循环中对数组进行排序,并使用已排序的值填充新数组

I have an array, $items, which contains lot of informations: name, size, quantity, color, id... I'm using 'for' loops to sort $items values by color and save the sorted values in a new array, But the values I get in the generated array are false or not right ordered.

In the way to sort $items, I first created 3 arrays from $items

    <?php
    $total = $item_number; // total count of $items iterations
    $color_list_final = array_values(array_unique($color_list)); // color list without occurrences
    $name_list_final = array_values(array_unique($name_list)); // name list without occurrences
    ?>

Then used them as reference to compare to $item

    <?php
    echo 'TEST - 1 - SORT BY COLORS<br/>';

    $sorted_items = array();  // the new array to fill
    $count_sorting = array(); // will count the total of all iterations

    // SORT BY COLORS
    $countfrom0 = -1;
    for ($i = 0; $i < $color_list_count; $i++) 
    {
        $countfrom0++;
        $current_color = $color_list_final[$countfrom0];

        echo 'couleur :'.$i.' '.$current_color.'<br/>';

        //SAVING THE CURRENT COLOR GROUP VALUE IN THE NEW ARRAY
        $sorted_items[$i]['color'] = $current_color;

        $i3 = 0;
        for ($i2 = 0; $i2 <= $total; $i2++) 
        {
            //IF ITEM HAVE THE SAME COLOR THAN THE CURRENT COLOR
            if($item[$i2]['color'] == $color_list_final[$countfrom0])
            {
                echo trim($item[$i2]['name']).' - '.$item[$i2]['size'].' - '.$item[$i2]['color'].'<br/>';

                //SAVING THE ITEMS VALUES IN THE NEW ARRAY
                $line[$i3] = trim($item[$i2]['name']).' - '.$item[$i2]['size'].' - '.$item[$i2]['color'].'<br/>';
                $sorted_items[$i]['items'] = $line;

                $count_sorting[$i3] = $i3;

                $i3++;
             }
        }
        echo '---------------------------<br/>';    
    }
    echo '<br/>';
?>

So echo are displaying the structure that I wanted

    COULEUR :0  #A0C343
    FREE BEES -  XL -  #A0C343
    FREE BEES -  M -  #A0C343
    COLOMBUS -  XS -  #A0C343
    FREE BEES -  XXXL -  #A0C343
    ---------------------------
    COULEUR :1  #FFE673
    FREE BEES -  M -  #FFE673
    ---------------------------
    COULEUR :2  #F7D8D3
    COLOMBUS -  XS -  #F7D8D3
    COLOMBUS -  S -  #F7D8D3
    ---------------------------

Now, when I'm doing a print_r on $sorted_items, I get the right number of array by color (3), but the nested array $line seems to keep the size and values of his first passage, and then, for the others passages, it replace the old values by the new ones who have the same index... but old values who'll have a bigger index than the new values, will remain the same as for the first passage.

ARRAY
(
[0] => ARRAY
    (
        [COLOR] =>  #A0C343
        [ITEMS] => ARRAY
            (
                [0] => FREE BEES -  XL -  #A0C343
                [1] => FREE BEES -  M -  #A0C343
                [2] => COLOMBUS -  XS -  #A0C343
                [3] => FREE BEES -  XXXL -  #A0C343
            )

    )

[1] => ARRAY
    (
        [COLOR] =>  #FFE673
        [ITEMS] => ARRAY
            (
                [0] => FREE BEES -  M -  #FFE673
                [1] => FREE BEES -  M -  #A0C343
                [2] => COLOMBUS -  XS -  #A0C343
                [3] => FREE BEES -  XXXL -  #A0C343
            )

    )

[2] => ARRAY
    (
        [COLOR] =>  #F7D8D3
        [ITEMS] => ARRAY
            (
                [0] => COLOMBUS -  XS -  #F7D8D3
                [1] => COLOMBUS -  S -  #F7D8D3
                [2] => COLOMBUS -  XS -  #A0C343
                [3] => FREE BEES -  XXXL -  #A0C343
            )

    )

)

Any Ideas?

  • 写回答

2条回答 默认 最新

  • duanrou5680 2013-07-13 15:26
    关注

    I emptied the array ‘$line‘ after the nested ‘for‘ loop

    $i3 = 0;
    for ($i2 = 0; $i2 <= $total; $i2++) 
    {
        //SI LA COULEUR DE L ITEM EST LA MM QUE CELLE PRECEDEMENT DEFINI
        if($item[$i2]['color'] == $color_list_final[$countfrom0])
        {
            echo trim($item[$i2]['name']).' - '.$item[$i2]['size'].' - '.$item[$i2]['color'].'<br/>';
    
            $line[$i3] = trim($item[$i2]['name']).' - '.$item[$i2]['size'].' - '.$item[$i2]['color'];
            $sorted_items[$i]['items'] = $line;
    
            $count_sorting[$i3] = $i3;
    
            $i3++;
        }
    }
    $line = array();
    

    And it gives me exactly what I wanted

    ARRAY
    (
    [0] => ARRAY
        (
            [COLOR] =>  #A0C343
            [ITEMS] => ARRAY
                (
                    [0] => FREE BEES -  XL -  #A0C343
                    [1] => FREE BEES -  M -  #A0C343
                    [2] => COLOMBUS -  XS -  #A0C343
                    [3] => FREE BEES -  XXXL -  #A0C343
                )
    
        )
    
    [1] => ARRAY
        (
            [COLOR] =>  #FFE673
            [ITEMS] => ARRAY
                (
                    [0] => FREE BEES -  M -  #FFE673
                )
    
        )
    
    [2] => ARRAY
        (
            [COLOR] =>  #F7D8D3
            [ITEMS] => ARRAY
                (
                    [0] => COLOMBUS -  XS -  #F7D8D3
                    [1] => COLOMBUS -  S -  #F7D8D3
                )
    
        )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器