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?