0

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

Sign up to request clarification or add additional context in comments.

2 Comments

First thanks you for your answer, and then sorry for the delay!
$i3 counter needs to be incremented only if the if condition is true, that's why I set it alone. I tried your suggestion with some modification and it worked in a certain way (I'll edit my post in few minutes), but the 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.
0

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
            )

    )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.