1

I have an array like this:

[0] => array([location] => [10.1111111,106.2222222];);

[1] => array([location] => [10.1111111,106.2222222];)

[2] => array([location] => [10.3333333,106.444444444];)

[3] => array([location] => [10.1111111,106.2222222];)

[4] => array([location] => [10.3333333,106.444444444];)

I want to keep first array value ( of duplicate values) , and replace all the duplicate value remaining with random number

[0] => array([location] => [10.1111111,106.2222222];)

[1] => array([location] => [10.54545422,106.136633434];)

[2] => array([location] => [10.3333333,106.444444444];)

[3] => array([location] => [10.323123232,106.656565654];)

[4] => array([location] => [10.44342266,106.87878787];)

my code but it's not seem to work:

foreach($dataMap as $items) {

            $temp = array();
            foreach($items as $value) {

                if(!isset($temp[$value])) {
                    $temp[$value] = '[10.888888,106.999999]';
                }
            }
            $items['location'] = $temp;
        }
paul smith
  • 13
  • 4

1 Answers1

-1

Usually I create a list or set and validate each value to that list, if exist then replace the decimal values and add to the list or set else add the value, by this we achieve unique values.

This is how I do it in python.

import random

arr = list()
arr.append([10.1111111, 106.2222222])
arr.append([10.1111111, 106.2222222])
arr.append([10.3333333,106.444444444])
arr.append([10.1111111, 106.2222222])

print(arr)

# Set to check duplicates..
dup = set()
temp_arr = list()

for each_row in range(len(arr)):
    temp_arr.append([])
    for each_col in range(len(arr[each_row])):
        val = arr[each_row][each_col]
        if val in dup:  # found duplicate
            my_int = int(val)
            # new value
            new_val = round(random.uniform(my_int, my_int+1), 7)
            temp_arr[each_row].append(new_val)
            dup.add(new_val)
        else:
            dup.add(val)  # non duplicates.
            temp_arr[each_row].append(val)
print(temp_arr)
Kiran Kumar Kotari
  • 1,078
  • 1
  • 10
  • 24
  • While this code may answer the question, providing additional context regarding **how** and/or **why** it solves the problem would improve the answer's long-term value. – Alexander Apr 05 '18 at 05:43
  • @Alexander you can find the details in the comments. It's good enough to understand. – Kiran Kumar Kotari Apr 05 '18 at 06:40