Nick
Nick

Reputation: 1444

Remove duplicates from different JSON Arrays

I have a JSON structure that has a lot of arrays.I want to check if there are duplicates.Not inside the same array but in different arrays.Also i want the other fields to stay as is.

Here is an example of my structure:

{
  "Collection":[
    {
    "field0":"string",
    "field1":"string",
    "field2":"string",
    "field3":"string",
    "field4":"string",
    "field5":"string",
    "field6":"string",
    "field7":"string",
    "field8":"string",
      "field9":[
         "test1"
         "test2"
         "test3"
       ]
    },
    {
    "field0":"string",
    "field1":"string",
    "field2":"string",
    "field3":"string",
    "field4":"string",
    "field5":"string",
    "field6":"string",
    "field7":"string",
    "field8":"string",
      "field9":[
         "test8"
         "test2"
         "test9"
       ]
    }
  ]
}

And here is what I expect:

{
  "Collection":[
    {
    "field0":"string",
    "field1":"string",
    "field2":"string",
    "field3":"string",
    "field4":"string",
    "field5":"string",
    "field6":"string",
    "field7":"string",
    "field8":"string",
      "field9":[
         "test1"
         "test2"
         "test3"
       ]
    },
    {
    "field0":"string",
    "field1":"string",
    "field2":"string",
    "field3":"string",
    "field4":"string",
    "field5":"string",
    "field6":"string",
    "field7":"string",
    "field8":"string",
      "field9":[
         "test8"
         "test9"
       ]
    }
  ]
}

I don't know if this is relevant but this is a firestore collection.

Upvotes: 0

Views: 66

Answers (1)

Serghei Niculaev
Serghei Niculaev

Reputation: 513

O'k, I don't know Swift, but I can show you how to do it:

<?php

$json = <<<JSON
{
    "Collection":[
        {
            "field1":[
                "test1",
                "test2",
                "test3"
            ]
        },
        {
            "field2":[
                "test8",
                "test2",
                "test9"
            ]
        }
    ]
}
JSON;

$entities = json_decode($json, true); // Decode JSON

$collection = $entities['Collection']; // Grab array of fields inside collection
$elements = []; // Initialize an empty array of unique elements
$result = [];
foreach ($collection as $index => $fieldObject) {
    $fieldName = array_keys($fieldObject)[0]; // Get field name

    // Get value from array of values of this field
    foreach ($fieldObject[$fieldName] as $valueKey => $value) {

        // Check if your value is not in array of unique elements
        if (!in_array($value, $elements)) {
            $elements[] = $value; // Add value if is not

            // Add value to your new array
            $result['Collection'][$index][$fieldName][] = $fieldObject[$fieldName][$valueKey];
        }
    }
}

$result = json_encode($result); // Encode it back to JSON

Here is the working example in sandbox: http://sandbox.onlinephpfunctions.com/code/a78cff49cc31c15e7e1373f7e1f66b7951f129e9

Upvotes: 1

Related Questions