Unique array values in foreach

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • m|sf|t

    Unique array values in foreach

    All,
    I have a snippet of code below. In my foreach, I would like to process only
    1 item per hdd value, in the case below, the echo $tib['hdd'] . "\n"; would
    only display 1 and 2, not 1,1,1,1,2,2,2,2 . It doesn't matter which one
    shows, because I only want the unique value for hdd.

    $tibs = array(
    "tib1" => array("drive" => "C", "progress" => "on", "hdd" => "1",
    "partition" => "1", "compressio n" => "3", "backup" => "K:\\"),
    "tib2" => array("drive" => "D", "progress" => "on", "hdd" => "1",
    "partition" => "2", "compressio n" => "3", "backup" => "K:\\"),
    "tib3" => array("drive" => "E", "progress" => "on", "hdd" => "1",
    "partition" => "3", "compressio n" => "3", "backup" => "K:\\"),
    "tib4" => array("drive" => "F", "progress" => "on", "hdd" => "1",
    "partition" => "4", "compressio n" => "3", "backup" => "K:\\"),
    "tib5" => array("drive" => "G", "progress" => "on", "hdd" => "2",
    "partition" => "1", "compressio n" => "3", "backup" => "L:\\"),
    "tib6" => array("drive" => "H", "progress" => "on", "hdd" => "2",
    "partition" => "2", "compressio n" => "3", "backup" => "L:\\"),
    "tib7" => array("drive" => "I", "progress" => "on", "hdd" => "2",
    "partition" => "3", "compressio n" => "3", "backup" => "L:\\"),
    "tib8" => array("drive" => "J", "progress" => "on", "hdd" => "2",
    "partition" => "4", "compressio n" => "3", "backup" => "L:\\"),
    );

    foreach($tibs as $tib) {
    echo $tib['hdd'] . "\n";
    }

    Thanks for any help.


  • Frank Jones

    #2
    Re: Unique array values in foreach

    m|sf|t wrote:[color=blue]
    > foreach($tibs as $tib) {
    > echo $tib['hdd'] . "\n";
    > }
    >
    > Thanks for any help.[/color]

    $_tibs = array();
    foreach($tibs as $tib)
    {
    if ( !in_array($tib['hdd'], $_tibs) )
    {
    echo $tib['hdd'] . "\n";
    $_tibs[] = $tib['hdd'];
    }
    }


    Comment

    • m|sf|t

      #3
      Re: Unique array values in foreach

      whoa - that was smokin fast - thanks. i had to change $_tibs to $tibs to get
      it to work, but it's working great now.
      Thanks again =)

      "Frank Jones" <frank@spam.com > wrote in message
      news:42647de5$0 $246$61c65585@u q-127creek-reader-03.brisbane.pip enetworks.com.a u...[color=blue]
      > m|sf|t wrote:[color=green]
      >> foreach($tibs as $tib) {
      >> echo $tib['hdd'] . "\n";
      >> }
      >>
      >> Thanks for any help.[/color]
      >
      > $_tibs = array();
      > foreach($tibs as $tib)
      > {
      > if ( !in_array($tib['hdd'], $_tibs) )
      > {
      > echo $tib['hdd'] . "\n";
      > $_tibs[] = $tib['hdd'];
      > }
      > }
      >
      >[/color]


      Comment

      Working...