0

I have a some data stored in $data[], there are some items in it. How do I remove duplicate values in a foreach?

Array
(
    [0] => ABC
)
Array
(
    [0] => XZY
)
Array
(
    [0] => ABC
)

i have use some function array unique and convert it to json ... and not work

Zimo
  • 1
  • 1
  • Post the code pls. – Karlo Kokkak Mar 21 '18 at 09:57
  • 2
    Please add the code and provide more details in order to investigate the issue. – Darshan Jain Mar 21 '18 at 09:59
  • 1
    Please [edit] your question to include a [mcve]. Please read that link and make sure that the code you post in your question is minimal (only bare minimum of code necessary to reproduce), complete (all of the code necessary to reproduce) and verifiable (we can reproduce the issue using only the code in your question). JsFiddle links don't count as an MCVE. Without an MCVE in your question, this question is off-topic for StackOverflow. – Matan Shahar Mar 21 '18 at 10:30
  • i do not understand how update issue i create new post see here : https://stackoverflow.com/questions/49407050/how-remove-duplicate-value-in-php – Zimo Mar 21 '18 at 13:41

3 Answers3

2

Use array_unique($array) function in order to remove duplicate value. It takes an input array and returns a new array without duplicate values.

Darshan Jain
  • 479
  • 2
  • 10
  • 1
    What you want to achieve? Can you send me your code or script? I will do and let you know. – Darshan Jain Mar 21 '18 at 09:55
  • i think because i'am in a loop this is my code : `code` $cache = []; for($i=0;$i<=count(get_numerics($name));$i++){ $cc = get_numerics($name); $num = $cc[$i]; $cc = validatefunc($num); if (is_numeric($cc)) { if(trueresult($cc)) { if (!in_array($cc, $cache)){ $cache[] = $cc; } } } } – Zimo Mar 21 '18 at 10:06
  • @Zimo - Provide your array input and expected output so I can do as per your requirements. This code isn't be identify properly. – Darshan Jain Mar 21 '18 at 10:10
  • 1
    @Zimo please update your question with your code because we don't know what does your `get_numerics()`, `validatefunc()` and `trueresult()` functions. This answer is the correct one for the question you asked so far. – micster Mar 21 '18 at 10:15
0

You can use array_merge() first. Then on result do array_unique();

Karlo Kokkak
  • 3,674
  • 4
  • 18
  • 33
0

$input = array_map("unserialize", array_unique(array_map("serialize", $array)));

Rp9
  • 1,955
  • 2
  • 23
  • 31