2

the below are the array am getting out i want to mearge array and remove duplicats of it and pass too for loop not able too do i just want is remove duplicate and pass too for loop as a unique value

i tried with print_r(array_merge($uniq_arr));

for($i = 0; $i < count($uniq_arr); $i++) {

                        $tag = $uniq_arr[$i];
}

Array
(
    [0] => diet
    [1] => exercise
)
Array
(
    [0] => diet
    [1] => exercise
)
Array
(
    [0] => diet
    [1] => exercise
)
Array
(
    [0] => water intake
    [1] => hygiene
    [2] => diet
)

Out put in tag shold be like each unique value

Shaik
  • 930
  • 1
  • 20
  • 48

2 Answers2

2

You could just use array_merge() indiscriminately then use array_unique() to remove any duplicate entries.

M31
  • 1,348
  • 12
  • 16
  • 1
    @shaik - array_unique will not work on nested arrays as one may expect. If this is what you're referring to then you may need to give additional details as to what your initial data set looks like. If you have 4 arrays which contain strings you should be able to use array_merge() to create one array of all your strings before using array_unique() to remove duplicate entries – M31 Mar 28 '17 at 00:49
  • i did added a array merge – Shaik Mar 28 '17 at 00:52
  • se for me in my database i mage multiple values they are tag stored wht i want too do its check multiple tags with my unique tag table and show the only tags that are present in tag post table – Shaik Mar 28 '17 at 00:53
  • @Shaik - Can you edit your question to include that code? – M31 Mar 28 '17 at 00:53
  • can u join me on team viewer or skype i vl share u my screen ? – Shaik Mar 28 '17 at 00:54
0

Here is how you could implement the array_merge, array_unique solution. I am making an assumption that you have 4 arrays like so:

$tag1Array = array('diet','exercise');
$tag2Array = array('diet','exercise');
$tag3Array = array('diet','exercise');
$tag4Array = array('water intake','diet','exercise');

$commontags = array_unique(array_merge($tag1Array,$tag2Array,$tag3Array,$tag4Array));

This should result in a single array of the unique values present in any of the 4 tag arrays.

tjfo
  • 1,169
  • 1
  • 10
  • 12