Flattening Multidimensional array to unique values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chuy08

    Flattening Multidimensional array to unique values

    If I have a multidimensiona l array like the following:

    Array
    $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

  • Rik

    #2
    Re: Flattening Multidimensiona l array to unique values

    On Mon, 12 Feb 2007 18:43:26 +0100, Chuy08 <chuy08@yahoo.c omwrote:
    If I have a multidimensiona l array like the following:
    >
    Array
    $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?
    Why does you example not contain a multidimensiona l array? This one is
    handled just fine by array_unique(). ..

    You probably mean:
    $records = array(
    array('product' ='30 year','rate'=6. 0),
    array('product' ='30 year','rate'=6. 0),
    array('product' ='Pay option','rate'= 1.0),
    array('product' ='Pay option','rate'= 1.0));

    Quite some overhead, but workable (even preserves keys):
    <?php
    function array_unique_mu lti($array){
    $copy = $array;
    array_walk($arr ay,create_funct ion('&$v,$k','$ v = serialize($v);' ));
    $array = array_unique($a rray);
    return array_intersect _key($copy,$arr ay);
    }
    ?>

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

    Comment

    Working...