0
[{
    "sys_id": "2015-07-018",
    "account_id": "2015-07-018",
    "Names": [{
        "fname": "Jackie",
        "mname": "Lee",
        "lname": "Chan",
        "suffix": "Jr",
        "city": "Tokyo",
        "town": "Shinagawa-ku",
        "dstrct": "District 2",
        "street": "Jr",
        "contactnum": "1234"
    }, {
        "fname": "Jackie",
        "mname": "Lee",
        "lname": "Chan",
        "suffix": "Jr",
        "city": "Tokyo",
        "town": "Shinagawa-ku",
        "dstrct": "District 2",
        "street": "Jr",
        "contactnum": "1234"
    }, {
        "fname": "Jackie",
        "mname": "Lee",
        "lname": "Chan",
        "suffix": "Jr",
        "city": "Tokyo",
        "town": "Shinagawa-ku",
        "dstrct": "District 2",
        "street": "Jr",
        "contactnum": "1234"
    }, {
        "fname": "Jackie",
        "mname": "Lee",
        "lname": "Chan",
        "suffix": "Sr",
        "city": "Tokyo",
        "town": "Shinagawa-ku",
        "dstrct": "District 2",
        "street": "Sr",
        "contactnum": "1234"
    }, {
        "fname": "Jackie",
        "mname": "Lee",
        "lname": "Chan",
        "suffix": "Sr",
        "city": "Tokyo",
        "town": "Shinagawa-ku",
        "dstrct": "District 2",
        "street": "Sr",
        "contactnum": "1234"
    }, {
        "fname": "Jackie",
        "mname": "Lee",
        "lname": "Chan",
        "suffix": "Sr",
        "city": "Tokyo",
        "town": "Shinagawa-ku",
        "dstrct": "District 2",
        "street": "Sr",
        "contactnum": "1234"
    }]
}]

THis my result from ajax request i want to remove the duplicate from the result either from server side or jquery side in my server side i have tried the most common way of removing duplicated array like

$input = array_map("unserialize", array_unique(array_map("serialize", $new_data)));
$new_data1 = array_values($input);
echo json_encode($new_data1, JSON_UNESCAPED_UNICODE);

Where in the $new_data is the result of my select query. The print_r($new_data) will result in

Array(
    [2015 - 07 - 018] => Array(
        [sys_id] => 2015 - 07 - 018[account_id] => 2015 - 07 - 018[Names] => Array(
            [0] => Array(
                [fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Jr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Jr[contactnum] => 1234)[1] => Array(
                [fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Jr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Jr[contactnum] => 1234)[2] => Array(
                [fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Jr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Jr[contactnum] => 1234)[3] => Array(
                [fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Sr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Sr[contactnum] => 1234)[4] => Array(
                [fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Sr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Sr[contactnum] => 1234)[5] => Array(
                [fname] => Jackie[mname] => Lee[lname] => Chan[suffix] => Sr[city] => Tokyo[town] => Shinagawa - ku[dstrct] => District 2[street] => Sr[contactnum] => 1234))))

Update

I tried print_r($new_data1); the result is exactly like the result of print_r($new_data);

Looking for a way to remove duplicate I found this

function super_unique($array) { $result = array_map("unserialize", array_unique(array_map("serialize", $array))); foreach($result as $key => $value) { if (is_array($value)) { $result[$key] = super_unique($value); } } return $result; }

The task i want to do now is to reindex the array because the output is

Array ( [1] => Array ( [sis_id] => 2015-07-018 [account_id] => 2015-07-018 [Names] => Array ( [0] => Array ( [fname] => Jackie [mname] => Lee [lname] => Chan [suffix] => Jr [city] => Tokyo [town] => Shinagawa-ku [brgy] => District 2 [contactnum] => 1234 ) [3] => Array ( [fname] => Jackie [mname] => Lee [lname] => Chan [suffix] => Sr [city] => Tokyo [town] => Shinagawa-ku [brgy] => District 2 [contactnum] => 1234 ) ) ) ) 

I want the old format for the out put

guradio
  • 15,524
  • 4
  • 36
  • 57

1 Answers1

0

What if you serialize each object in the array to a string (json would be easiest in my opinion) then use the native functions to remove duplicates, then unserialize back to objects?

Sudo code would look like this:

$serialized_array = [];
foreach($new_data as $data) {
    $serialized_array[] = json_encode($data);
}

$new_data = []; //reset new_data
$unique = array_unique($serialized_array);
foreach($serialized_array as $string) {
    $new_data[] = json_decode($string);
}
//new_data should now only contain unique data

The main difference between what you're doing and this is serializing and deserializing each object individually instead of the entire array. This is the key.

MrMadsen
  • 2,713
  • 3
  • 22
  • 31
  • do i need to change anything for the code because when i tried it the ouyput is still `http://pastebin.com/jd0HUW7Z` – guradio Jul 11 '15 at 04:50
  • change anything for what code? The code in my answer is only sudo code using php syntax so it will probably need some tweeks to work. – MrMadsen Jul 11 '15 at 04:54