0

我有一个数组,$items其中包含很多信息:名称、大小、数量、颜色、id ... 我正在使用“for”循环$items按颜色对值进行排序并将排序后的值保存在一个新数组中,但是值我在生成的数组中得到的是假的或者没有正确排序。

在排序的方式中$items,我首先从 $items 创建了 3 个数组

    <?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
    ?>

然后用它们作为参考来比较$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/>';
?>

所以 echo 正在显示我想要的结构

    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
    ---------------------------

现在,当我在 print_r 上执行时$sorted_items,我通过颜色 (3) 获得了正确数量的数组,但嵌套数组$line似乎保留了他的第一个段落的大小和值,然后,对于其他段落,它替换具有相同索引的新值的旧值......但是具有比新值更大的索引的旧值将与第一段保持相同。

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
            )

    )

)

有任何想法吗?

4

2 回答 2

1

if it is echoing in the right order then try to populate an array with the data at the point where you echo it. this is quite a long thread to debug without using the code itself, but perhaps you could try to turn

echo trim($item[$i2]['name']).' - '.$item[$i2]['size'].' - '.$item[$i2]['color'].'<br/>';

into

$array[$i2] = array(  'name'  => trim($item[$i2]['name'])
                   ,  'item'   => $item[$i2]['size']
                   ,  'color' => $item[$i2]['color']
                   );

also, $i3's role seems a little ambiguous so maybe either declare it in a for() loop or get rid of it altogether.

于 2013-07-11T11:06:30.500 回答
0

我在嵌套的“for”循环之后清空了数组“$line”

$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();

它给了我我想要的东西

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
            )

    )

)
于 2013-07-13T15:26:23.073 回答