Unique multi dimensional arrays

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I have worked for some time on developing a method for this issue. The
problem has always been the flexibility of the method to work in any
situation.

I had initially thought this might be my solution.

Function multi_dim_unique($array='')

{

                For ($a=0;$b=count($array);$a<$b;$a++) {

                                For ($c=0,$d=count($array[$a]);$c<$d;$c++) {

                                $tmp = $array[$a][$c];

                                               If (!isset
($record_array[$tmp])) {

                                                $record_array[$tmp] = $c;

                                                }

                                }

                }

                Foreach ($record_array as $key=>$value) {

                                $array[$value][] = $key;

                }

Return $array;

}

While this concept work perfect for multi dimensional arrays like.

$array = (0=>array(0=>12345,1=>67890),1=>array(0=>12345,1=>67890,2=>11111));

 

I find myself constantly needing to dig deeper and unique at other positions
of the array but still have the flexibility.

$array = array([0] = > array(['Company'] => array( [0] = > ABC,[1] =>
CBS,[2] => NBC), [Owners] => array ( [0] => John,[1] => Mark,[2] => John));

 

In a desire to find the unique Owner names from the array above. The initial
method is not accommodating for this array and will return the entire array
as presented.

My new thoughts are to pass a parameter to the method for the position I
want unique.

 

$position = 'Owners';

multi_dim_unique($array,$postion);

 

Here lies the problem finding the position match no matter how many
dimensions there are.

My question is has anyone tackled this issue and may have some suggestions
as to how I may reconstruct the method to be a bit more accommodating to
situations like this. 

 

 

 

Richard L. Buskirk

 


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux