note 37248 deleted from function.array-merge by levim

From: Date: Mon, 24 Feb 2014 22:51:58 +0000
Subject: note 37248 deleted from function.array-merge by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198672@lists.php.net to get a copy of this message
Note Submitter: KOmaSHOOTER at gmx dot de ---- Here is something for splitting a sorted array into group array ( sorting the group arrays ) and then merging them together Note: merge is like concat in Flash ActionScript :) In this example i have 3 different groups in one array * First the mainArray is sorted for the group entrys * building from part arrays * building copy arrays from part arrays ( for making the array beginning with [0] !! ) * merging all part arrays together 0. sorting the main array 1. apportionment from the main array when the group is changing , a new array is build (part array ) ! attention at the second part array !! Array values are are not beginning with [0]!! 2. making the copy from the part array . array values are pushed in the new array 3. echo for every array entry 4. overwriting the old array with the new copy 5. part arrays are sorted by function (arrays had been naughted by function array_copy ( Array is beginning with [0] ! ) 6. merging the part arrays Here is the same that i posted before but a bit better solved array_push (${"activ_array".$j}, $activ_array[$i]); makes the part arrays starting with [0] :) <?php $activ_array=array( array("apfel_01","germany","xx","red"), array("apfel_07","germany","xx","green"), array("apfel_08","germany","xx","green"), array("apfel_02","germany","xx","red"), array("apfel_03","germany","xx","red"), array("apfel_04","germany","xx","red"), array("apfel_09","germany","xx","red_+_green"), array("apfel_10","germany","xx","red_+_green"), array("apfel_05","germany","xx","green"), array("apfel_06","germany","xx","green") ); function array_sort($array, $key , $target){ for ($i = 0; $i < sizeof($array); $i++){ $array2 = $array; if($target == 'name'){ if(! empty($array2[$i]["nameletter"])){ //echo $myarray[$i]["nameletter"] ; $array2[$i]["name"] = substr($array2[$i][$key],($array2[$i]["nameletter"]-1)); }else{ $array2[$i]["name"] = substr($array2[$i][$key],0); } $sort_values[$i] = $array2[$i]["name"]; }else{ $sort_values[$i] = $array2[$i][$key]; } } asort ($sort_values); reset ($sort_values); while (list ($arr_keys, $arr_values) = each ($sort_values)) { $sorted_arr[] = $array[$arr_keys]; } return $sorted_arr; } $activ_array = array_sort($activ_array,3,'imagetype'); $j=0; // first part array ${"activ_array"."$j"} = array(); for($i=0;$i<sizeof($activ_array);$i++){ if($activ_array[$i][3] !=$activ_array[$i-1][3] && $i >1){ $j++; ${"activ_array".$j} = array(); } array_push (${"activ_array".$j}, $activ_array[$i]); } // part array sorting for($i=0;$i<=$j;$i++){ ${"activ_array".$i} = array_sort(${"activ_array".$i},0,'name'); } $activ_array = array(); for($i=0;$i<=$j;$i++){ $activ_array = array_merge($activ_array,${"activ_array".$i}); } for($i=0;$i<sizeof($activ_array);$i++){ print_r($activ_array[$i]); echo("<br>"); } ?>

« previous php.notes (#198672) next »