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.

everywhere I found solutions for removing duplicate entries in php or mysql. But all those solutions removed the whole row, but I just want to remove the value of a column. for example I have this SELECT in mysql:

SELECT HE_FRUIT as fruit, CA_FRUIT as candy, CA_FRUIT_NAME as name, CA_FRUIT_color as color 
FROM fruits LEFT JOIN candy ON HE_FRUIT_ID = CA_FRUIT_ID 
WHERE CA_FRUIT_COUNTRY LIKE '%usa%' 
GROUP BY CA_FRUIT_TITLE 
ORDER BY CA_FRUIT_PRIO, CA_FRUIT_HE

I print the array and I get this:

array
  0 => 
      array
      fruit = "APPLE"
      candy = "lolly"
      name = "xxx"
      color = "green"
   1 => 
      array
      fruit = "APPLE"
      candy = "gum"
      name = "xxx"
      color = "blue"
  2 => 
      array
      fruit = "APPLE"
      candy = "candy"
      name = "xxx"
      color = "red"
  3 => 
      array
      fruit = "BANANA"
      candy = "lolly"
      name = "xxx"
      color = "yellow"

What I want to have is:

  0 => 
      array
      fruit = "APPLE"
      candy = "lolly"
      name = "xxx"
      color = "green"
   1 => 
      array
      fruit = ""
      candy = "gum"
      name = "xxx"
      color = "blue"
  2 => 
      array
      fruit = ""
      candy = "candy"
      name = "xxx"
      color = "red"
  3 => 
      array
      fruit = "BANANA"
      candy = "lolly"
      name = "xxx"
      color = "yellow"

So it's only the value of the column fruit that should be removed, but all the rest should stay.

Does someone has an idea how I could solve this problem, without to much extra code?

Thanks so much in advance?

share|improve this question
    
Do you have a large number of entries ? –  aurel.g Feb 9 '12 at 9:05
    
It depends, now I don't have much entries, but in the future there is a possibility that more entries will added. (I just don't know how much) –  user993177 Feb 9 '12 at 9:08

3 Answers 3

up vote 1 down vote accepted

Try this :

<?php

    $data = array(
      0 => array(
          'fruit' => "APPLE",
          'candy' => "lolly",
          'name' => "xxx",
          'color' => "green"
      ),
      1 => array(
          'fruit' => "APPLE",
          'candy' => "gum",
          'name' => "xxx",
          'color' => "blue"
      ),
      2 => array(
          'fruit' => "APPLE",
          'candy' => "candy",
          'name' => "xxx",
          'color' => "red"
      ),
      3 => array(
          'fruit' => "BANANA",
          'candy' => "lolly",
          'name' => "xxx",
          'color' => "yellow"
      )
    );

    foreach($data as $i => $dataRow)
    {
        foreach($dataRow as $fieldName => $fieldValue)
        {
            for($j=0; $j<$i; $j++)
            {
                if($data[$j][$fieldName] == $fieldValue)
                {
                    $data[$i][$fieldName] = '';
                }
            }
        }
    }

    print_r($data);

?>
share|improve this answer
    
This worked wonderfully. Thank you very much! –  user993177 Feb 9 '12 at 9:21
    
Note this is quite a fragile solution. It relies on the 'fruit' being ordered. If not, then it doesn't produce correct output (test by inserting a 'BANANA' sub-array amongst the 'APPLE's). Might work for now but the dependency on the query ordering isn't obvious and might easily break/change in the future. –  liquorvicar Feb 9 '12 at 13:22

Do you need the result array to be only single-dimensional? If not, I would suggest you could process the array into a multi-dimensional array, like this:

$array=array(
  0 => 
      array(
      'fruit' => "APPLE",
      'candy' => "lolly",
      'name' => "xxx",
      'color' => "green"),
  1 => 
      array(
      'fruit' => "APPLE",
      'candy' => "gum",
      'name' => "xxx",
      'color' => "blue"),
  2 => 
      array(
      'fruit' => "APPLE",
      'candy' => "candy",
      'name' => "xxx",
      'color' => "red"),
  3 => 
      array(
      'fruit' => "BANANA",
      'candy' => "lolly",
      'name' => "xxx",
      'color' => "yellow")
);

$output = array();
foreach( $array as $arrayItem ) {
    if( !isset( $output[$arrayItem['fruit']]) ) {
        $output[$arrayItem['fruit']] = array();
    }
    $outputItem = $arrayItem;
    unset( $outputItem['fruit'] );
    $output[$arrayItem['fruit']][] = $outputItem;
}

You could achieve the same effect by looping through the original data set, you don't need to create a one-dimensional PHP array and then process it again. This is just to make the example clearer.

share|improve this answer
    
This was also a useful solution, thank you very much. –  user993177 Feb 9 '12 at 9:32

SELECT DISTINCT or array_unique() and friends won't help you here: You do not want to suppress duplicate rows, you want to manipulate them to have a different fruit: the empty string.

So you will end up needing a loop:

//Assumes your resultset is in $myarray
$fruits=array();

for ($i=0;$i<sizeof($myarray);$i++)
  if (in_array($myarray[$i]['fruit'],$fruits)) $myarray[$i]['fruit']='';
  else $fruits[]=$myarray[$i]['fruit'];
share|improve this answer

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.