Jump to content

Copy Machine-Esque Collation of an Array - Not Your Mother's Collation


cobraroll98

Recommended Posts

Hello,

 

I have an array which contains the values:

1,2,3,4,5,5,4,3,2,1,1,2,3,4,5,3,2

 

Assume each array value is a number 1-1000, and is not pushed into the array in any relevant order.

 

I would like to display the array values in the following order:

1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,2,3

 

I have had some difficulty writing/finding a function which will order things in this fashion.

 

Ideally I want to use this function to display results from a Postgres database but since the db's result set returns an array I figured this was a good place to start. 

 

I'm not a very good math person, just a functional programmer, so any help would be awesome!

Thanks in advance.

Martin.

 

As far as rules I'm afraid I'm not sophisticated enough to know what you're asking.

Perhaps this helps:

 

The each resulting array value should not be the same as the value before or after it

unless there are no alternatives.

 

try

<?php
function my_order($a){
if (!is_array($a)) return false;
$out = array();
while (count($a)){
	$tmp = array_unique($a);
	sort($tmp);
	$out = array_merge($out, $tmp);
	foreach ($tmp as $val){
		unset($a[array_search($val,$a)]);
	}
}
return $out;
}
$start = '1,2,3,4,5,5,4,3,2,1,1,2,3,4,5,3,2';
$start = explode(',',$start);
echo implode(',', my_order($start));
?>

OK, I thought this was an interesting problem, so I decided to solve it another way. Here's another solution - don't know if it is more or less efficient than Sasa's

 

function collate($arry)
{
    if (!is_array($arry)) return false;
    $out = array();
    $values = array_count_values($arry);
    ksort($values);
    while (count($values)) {
        $out = array_merge($out, array_keys($values));
        foreach($values as $key => $value) $values[$key]--;
        $values = array_filter($values);
    }
    return $out;
}

sasa, mjdamato,

 

Thank you so much for the help your functions work great!

Is there an easy way to perform this on a multidimensional array specifying

the array 'column' that contains the number to be sorted?

 

mjdamato: It seems the array_count_values function doesn't work with multid arrays so I've  been looking for a substitute.

 

Here's an example of my multiD array, I would like to perform the collate function based on the value of the ID column.  Thank you so much once again!

 

$start =array();

array_push($start,array("ID" => 1, "Name" => Fish));

array_push($start,array("ID" => 2, "Name" => dog));

array_push($start,array("ID" => 3, "Name" => boat));

array_push($start,array("ID" => 4, "Name" => hat));

array_push($start,array("ID" => 5, "Name" => smurf));

array_push($start,array("ID" => 5, "Name" => toad));

array_push($start,array("ID" => 5, "Name" => princess));

 

try

<?php
$start =array();
array_push($start,array("ID" => 1, "Name" => Fish));
array_push($start,array("ID" => 2, "Name" => dog));
array_push($start,array("ID" => 5, "Name" => boat));
array_push($start,array("ID" => 4, "Name" => hat));
array_push($start,array("ID" => 3, "Name" => smurf));
array_push($start,array("ID" => 2, "Name" => toad));
array_push($start,array("ID" => 5, "Name" => princess));
//reorganize array
$new_start = array();
foreach ($start as $elemen){
$new_start[$elemen['ID']][] = $elemen;
}
$out = array();
ksort($new_start);
while (count($new_start) and $i++ <11){
foreach ($new_start as $id => $arr_elem){
	array_push($out, array_shift($new_start[$id]));
	if (count($new_start[$id]) == 0) unset($new_start[$id]);
}
}
print_r($out);
?>

sasa, mjdamato,

 

Thank you so much for the help your functions work great!

Is there an easy way to perform this on a multidimensional array specifying

the array 'column' that contains the number to be sorted?

 

mjdamato: It seems the array_count_values function doesn't work with multid arrays so I've  been looking for a substitute.

 

Here's an example of my multiD array, I would like to perform the collate function based on the value of the ID column.  Thank you so much once again!

 

$start =array();

array_push($start,array("ID" => 1, "Name" => Fish));

array_push($start,array("ID" => 2, "Name" => dog));

array_push($start,array("ID" => 3, "Name" => boat));

array_push($start,array("ID" => 4, "Name" => hat));

array_push($start,array("ID" => 5, "Name" => smurf));

array_push($start,array("ID" => 5, "Name" => toad));

array_push($start,array("ID" => 5, "Name" => princess));

 

I wouldn't set up my array like that, to be honest.  array_push() is kind of slow compared to just using the [] notation:

$start = array();
$start[] = array('ID' => 4, 'Name' => 'hat');
//etc

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.