-1

I'm getting strange behavior of array. I am getting an array with duplicate keys. I checked that this is due to the Datatype of the keys. How can i make it unique. This is the array:

Array
(
    [1811258] => stdClass Object
        (
            [status] => true
            [count] => 1
            [price] => 2501
            [attributes] => 
            [groupid] => 2400
            [subgroupid] => bux_364905
            [desc] => GE Refrigerator - Side by Side GC5SHEXNQ..
            [id] => 1811258
        )

    [1812193] => stdClass Object
        (
            [status] => true
            [count] => 1
            [price] => 293
            [attributes] => 
            [groupid] => 2410
            [subgroupid] => 0
            [desc] => STAINLESS STEEL DOUBLE BOWL - 20 GAUGE
            [id] => 1812193
        )

    [1811258] => stdClass Object
        (
            [status] => 1
            [count] => 1
            [price] => 2501
            [attributes] => 
            [groupid] => 2400
            [subgroupid] => bux_364905
            [desc] => GE Refrigerator - Side by Side GC5SHEXNQ..
            [id] => 1811258
        )

    [1852936] => stdClass Object
        (
            [status] => 1
            [count] => 1
            [price] => 525
            [attributes] => 
            [groupid] => 2489
            [subgroupid] => 0
            [desc] => 
            [id] => 1852936
        )

    [1812193] => stdClass Object
        (
            [status] => 1
            [count] => 1
            [price] => 293
            [attributes] => 
            [groupid] => 2410
            [subgroupid] => 0
            [desc] => STAINLESS STEEL DOUBLE BOWL - 20 GAUGE
            [id] => 1812193
        )

)

I have tried array_key_exists() function to check:

$saved=(array)json_decode($reviewdata->options);
 foreach($optioncodes as $key=>$optioncode){
        $option=$wpdb->get_row("SELECT * from {$prefix}builder_phaseplanoption where OptionCode='{$optioncode}' and SubdivisioNID='{$subdivision}'");
        if($option){
            if(array_key_exists($option->ID,$saved))
                $saved[$option->ID]=(object)array('status'=>true,'count'=>1,'price'=>$option->UnitPrice,'attributes'=>'','groupid'=>$option->OptionGroupID,'subgroupid'=>$option->Sub_OptionGroupID,'desc'=>$option->OptionLongDesc,'id'=>$option->ID);
        }
    }

But no effect.I also tried to change the data type.Like this

$optid=(string)$option->ID; // now this shoud string but no effect
                    $saved[$optid]=(object)array('status'=>true,'count'=>1,'price'=>$option->UnitPrice,'attributes'=>'','groupid'=>$option->OptionGroupID,'subgroupid'=>$option->Sub_OptionGroupID,'desc'=>$option->OptionLongDesc,'id'=>$option->ID);

I know this is due to the problem that keys in the saved array are strings while the new keys are numbers. How can i convert them to the same type.

Thanks in advance.

Don
  • 3,876
  • 10
  • 47
  • 76
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68
  • It will remove the keys.No? – Manoj Dhiman Jun 22 '17 at 17:10
  • Sorry in last comment it's a `array_unique()` try it. – Nirav Joshi Jun 22 '17 at 17:11
  • Possible duplicate of [How to remove duplicate keys in array](https://stackoverflow.com/questions/7550826/how-to-remove-duplicate-keys-in-array) – Anoop Toffy Jun 22 '17 at 17:15
  • *"I am getting an array with duplicate keys."* -- there are two possibilities: either you have found a serious bug in PHP, which is practically impossible, or there is a problem in your code. – axiac Jun 22 '17 at 17:16
  • Sorry i think it was you actual array – Nirav Joshi Jun 22 '17 at 17:17
  • @axiac I have mentioned in my question that "I know this is due to the problem that keys in the saved array are strings while the new keys are numbers. How can i convert them to the same type" – Manoj Dhiman Jun 22 '17 at 17:17
  • @NiravJoshi [`array_unique()`](http://php.net/manual/en/function.array-unique.php) removes the duplicate **values** from an array. There is no function to remove the duplicate keys because there is no way to have duplicate keys in an array. – axiac Jun 22 '17 at 17:18
  • @axiac its possible if data type is not save.int 123 and string 123 can be saved as keys in array. This is my case – Manoj Dhiman Jun 22 '17 at 17:19
  • @MKD It is still not possible to have duplicate keys using different types. The string keys that contain only digits and are valid base-10 numbers are automatically converted to numbers by PHP when it puts them in the array. Check this: https://3v4l.org/6l776 – axiac Jun 22 '17 at 17:20
  • then why these are duplicates? You can check my printed array. – Manoj Dhiman Jun 22 '17 at 17:22
  • @axiac if your comment is true then how it works when i change the datatype? Check my answer. – Manoj Dhiman Jun 22 '17 at 17:28
  • Post the value of `$reviewdata->options`. – axiac Jun 22 '17 at 17:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147397/discussion-between-axiac-and-mkd). – axiac Jun 22 '17 at 17:28
  • Its a json object {"1811258":{"status":"true","count":"1","price":"2501","attributes":"","groupid":"2400","subgroupid":"bux_364905","desc":"GE Refrigerator - Side by Side GC5SHEXNQ..","id":"1811258"},"1812193":{"status":"true","count":"1","price":"293","attributes":"","groupid":"2410","subgroupid":"0","desc":"STAINLESS STEEL DOUBLE BOWL - 20 GAUGE","id":"1812193"}} – Manoj Dhiman Jun 22 '17 at 17:30
  • Please check this https://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php link, I think it will be help. – Md. Nashir Uddin Jun 22 '17 at 19:17

1 Answers1

0

I have done this but i am not sure if it is the correct way to do this. If there is any better way then please give your suggestions. I changed the data type and create a new array.And it remove the duplicate values.

$newsaved=array();
foreach($saved as $key=>$value){
    $stroptid=(string)$key;
    $newsaved[$stroptid]=$value;
}

Now this is the output

Array
(
    [1811258] => stdClass Object
        (
            [status] => 1
            [count] => 1
            [price] => 2501
            [attributes] => 
            [groupid] => 2400
            [subgroupid] => bux_364905
            [desc] => GE Refrigerator - Side by Side GC5SHEXNQ..
            [id] => 1811258
        )

    [1812193] => stdClass Object
        (
            [status] => 1
            [count] => 1
            [price] => 293
            [attributes] => 
            [groupid] => 2410
            [subgroupid] => 0
            [desc] => STAINLESS STEEL DOUBLE BOWL - 20 GAUGE
            [id] => 1812193
        )

    [1852936] => stdClass Object
        (
            [status] => 1
            [count] => 1
            [price] => 525
            [attributes] => 
            [groupid] => 2489
            [subgroupid] => 0
            [desc] => 
            [id] => 1852936
        )

)
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68
  • The JSON you posted in the question's comments contains invisible characters and it's invalid. I wonder how you managed to decode it. `json_decode()` refuses to decode it for me: https://3v4l.org/cg5WC – axiac Jun 22 '17 at 17:50