Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array that looks like this:

$rowarray(
[0] => [PID] => 97162 [TID] => 340 [StatsID] => 49678
[1] => [PID] => 97165 [TID] => 340 [StatsID] => 49673
[2] => [PID] => 97167 [TID] => 340 [StatsID] => 49675
[3] => [PID] => 97162 [TID] => 340 [StatsID] => 49679
)

Then my code looks like this:

$cntr=0;
foreach($rowarray as $row)
{
 echo "<tr><td>$row[PID] $row[TID] $row[StatsID] </td></tr>";
$cntr++;
}

Two things I want to do I want to be able not print the duplicates in the array but print the additional column that has a different value. So my desired output would look like this.

97162 340 49678 49679

97165 340 49673

97167 340 49675

I started out with the array_unique() but that only returned:

97162 340 49678

share|improve this question
    
If this is data retrieved from a database query, you can use SELECT DISTINCT rather than simply SELECT –  Mark Baker Dec 28 '11 at 23:46
    
It is but the distinct doesn't display anything based on my statement. Also it would remove the entire row and not give me the different value. –  punkdis Dec 28 '11 at 23:49
    
Need more details... is it only the StatsID that changes? –  gotofritz Dec 28 '11 at 23:52
    
yes that is the only value that changes –  punkdis Dec 28 '11 at 23:55
    
If your data is returned from a database, you can use a simple GROUP BY and an aggregate function on your StatsID column. –  Kenaniah Dec 29 '11 at 1:10
show 1 more comment

4 Answers

up vote 0 down vote accepted

Assuming only the StatsID changes (not clear from the question)

$map = array();
foreach($rowarray as $row){
    $k = $row["PID"] . '-' . $row["TID"];
    if( !isset( $map[$k] ) ){
        $map[$k] = array();
    }
    array_push( $map[$k], $row["StatsId"] );
}


foreach($map as $k=>$v){
    $row = explode( '-', $k );
    echo "<tr><td>$row[0] $row[1] " . implode( " ", $v ) . " </td></tr>";
}
share|improve this answer
    
this one is what I used for reference thanks man –  punkdis Dec 29 '11 at 2:02
add comment

Here's what I'd do:

  • Start by sorting the array (using usort to sort by PID, then by TID)
  • Initialize "last" variables ($last_PID and $last_TID). They will store the respective values in the loop
  • In the loop, first compare the "current" variables to the "last" ones, if they're the same then just echo the StatsID value.
  • If they're not the same, output the <tr> (but not the final </tr>, so the first part of the loop can add more StatsID values if necessary)
  • Still inside the loop, after outputting everything, update the "last" variables.
  • After the loop, output the final </tr>

This may not be optimal, but I'm pretty sure it'll work.

share|improve this answer
add comment

Transfer the $rowarray structure into a map of maps of arrays, like this:

$rowarray = array(
    array('PID' => 97162, 'TID' => 340, 'StatsID' => 49678),
    array('PID' => 97165, 'TID' => 340, 'StatsID' => 49673),
    array('PID' => 97167, 'TID' => 340, 'StatsID' => 49675),
    array('PID' => 97162, 'TID' => 340, 'StatsID' => 49679)
);

$keys = array();

foreach ($rowarray as $row) {
    if (!is_array(@$keys[$row['TID']])) {
        $keys[$rowarray['TID']] = array();
    }

    if (!is_array(@$keys[$row['TID']][$row['PID']])) {
        $keys[$row['TID']][$row['PID']] = array();
    }

    $keys[$row['TID']][$row['PID']][] = $row['StatsID'];
}

foreach ($keys as $pid => $pid_arr) {
    foreach ($pid_arr as $tid => $tid_arr) {
        echo "<tr><td>$tid $pid " . implode(' ', $tid_arr) . "</td></tr>";
    }
}

See this code in action

share|improve this answer
1  
I liked the code in action section –  punkdis Dec 29 '11 at 2:02
add comment

As far as I can tell, the only way to do this would be to loop through the array creating a new unique array as you go.

$unique = array();
foreach ($row as $value)
{
    $key = $value['PID'];
    if (isset($unique[$key]))
    {
        $unique[$key]['StatsID'] .= ' ' . $value['StatsID'];
    }
    else
    {
        $unique[$key] = $value;
    }
}

Now, $unique would give you the results you're looking for and you can loop through the unique array and output your results (I also added your counter if needed):

$count = count($unique);
foreach ($unique as $row)
{
    echo "<tr><td>{$row['PID']} {$row['TID']} {$row['StatsID']} </td></tr>";
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.