You are here: Re: Flattening Multidimensional array to unique values « PHP Programming Language « IT news, forums, messages
Re: Flattening Multidimensional array to unique values

Posted by Rik on 02/12/07 18:20

On Mon, 12 Feb 2007 18:43:26 +0100, Chuy08 <chuy08@yahoo.com> wrote:

> If I have a multidimensional array like the following:
>
> Array
> $records =3D> Array
> 0 =3D> [Product] 30 year, [Rate]6.0;
> 1 =3D> [Product] 30 year, [Rate]6.0;
> 2 =3D> [Product] Pay Option, [Rate]1.0;
> 3 =3D> [Product] Pay Option, [Rate]1.0;
>
> How could I flatten this to achieve an array that only has unique Prod=
uct
> values, basically removing $records['1] and $records['3'] in this =

> example?

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

handled just fine by array_unique()...

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

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

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

Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация