0

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?

2
  • Do you have a large number of entries ? Commented Feb 9, 2012 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) Commented Feb 9, 2012 at 9:08

3 Answers 3

1

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);

?>
Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

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.

Comments

0

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'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.