note 37202 deleted from function.array-merge by didou
| From: | didou@php.net | Date: | Sun, 30 Nov 2003 23:21:17 +0000 |
| Subject: | note 37202 deleted from function.array-merge by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-61178@lists.php.net to get a copy of this message | ||
Note Submitter: KOmaSHOOTER@gmx.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
<?php
// sorting function
$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;
}
// 0.
$activ_array = array_sort($activ_array,3,'imagetype');
$j=0;
// first part array is build
${"activ_array"."$j"} = array();
for($i=0;$i<sizeof($activ_array);$i++){
// 1.
//******************************************
// imagetype = pos 3
if($activ_array[$i][3] !=$activ_array[$i-1][3] && $i >1){
$j++;
${"activ_array".$j} = array();
}
${"activ_array".$j}[$i] = $activ_array[$i];
}
//****************- Function - array_copy **
function array_copy($c3){
//***************- globale variablen ***
global $j;
for($i=0;$i<=$j;$i++){
global ${"activ_array".$i};
}
//***************- globale variablen ***
$c = $c3;
//echo("<br>------<br>"); // kopftrennlinie
// head line
// 2.
// new array is build
${"activ_array_copy".$c} = array();
//*************** foreach **
foreach (${"activ_array".$c} as $value){
//******Werte werden in neues Array gepushed ;)
array_push (${"activ_array_copy".$c}, $value);
//echo(" - ".$p." TestInG ");
// 3.
}
//*************** foreach **
// 4.
${"activ_array".$c} = ${"activ_array_copy".$c} ;
}
//****************- Function - array_copy **
//*** call: array_copy**
for($i=0;$i<=$j;$i++){
array_copy($i);
}
//*** call: array_copy**
// 5.
//########### array sortierung ##########
for($i=0;$i<=$j;$i++){
${"activ_array".$i} = array_sort(${"activ_array".$i},0,'name');
}
// 6.
//########### array sortierung ##########
//
//########### array mergen ##########
$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>");
}
?>