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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?