Flattening Multidimensional array to unique values

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
If I have a multidimensional array like the following:

$records => Array
                      0  => [Product] 30 year, [Rate]6.0;
                      1  => [Product] 30 year, [Rate]6.0;
                      2  => [Product] Pay Option, [Rate]1.0;
                      3  => [Product] Pay Option, [Rate]1.0;

How could I flatten this to achieve an array that only has unique Product  
values, basically removing $records['1] and $records['3'] in this example?  

Posted via a free Usenet account from http://www.teranews.com

Re: Flattening Multidimensional array to unique values

Quoted text here. Click to load it

Why does you example not contain a multidimensional array? This one is  =

handled just fine by array_unique()...

You probably mean:
$records = array(
      array('product' => '30 year','rate'=3D> 6.0),
      array('product' => '30 year','rate'=3D> 6.0),
      array('product' => 'Pay option','rate'=3D> 1.0),
      array('product' => 'Pay option','rate'=3D> 1.0));

Quite some overhead, but workable (even preserves keys):
function array_unique_multi($array){
    $copy = $array;
    array_walk($array,create_function('&$v,$k','$v = serialize($v);'));
    $array = array_unique($array);
    return array_intersect_key($copy,$array);

Be carefull not to have unserializable content in the array.
-- =

Rik Wasmus

Site Timeline