php - Sort an array in a for loop and fill a new array with the sorted values -


i have array, $items, contains lot of informations: name, size, quantity, color, id... i'm using 'for' loops sort $items values color , save sorted values in new array, values in generated array false or not right ordered.

in way sort $items, first created 3 arrays $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 reference compare $item

    <?php     echo 'test - 1 - sort colors<br/>';      $sorted_items = array();  // new array fill     $count_sorting = array(); // count total of iterations      // sort colors     $countfrom0 = -1;     ($i = 0; $i < $color_list_count; $i++)      {         $countfrom0++;         $current_color = $color_list_final[$countfrom0];          echo 'couleur :'.$i.' '.$current_color.'<br/>';          //saving current color group value in new array         $sorted_items[$i]['color'] = $current_color;          $i3 = 0;         ($i2 = 0; $i2 <= $total; $i2++)          {             //if item have same color current color             if($item[$i2]['color'] == $color_list_final[$countfrom0])             {                 echo trim($item[$i2]['name']).' - '.$item[$i2]['size'].' - '.$item[$i2]['color'].'<br/>';                  //saving items values in 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 displaying structure 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 print_r on $sorted_items, right number of array color (3), nested array $line seems keep size , values of first passage, , then, others passages, replace old values new ones have same index... old values who'll have bigger index new values, remain same 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?

if echoing in right order try populate array data @ point echo it. quite long thread debug without using code itself, perhaps try 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 little ambiguous maybe either declare in for() loop or rid of altogether.


Comments