-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.

15
  • It will remove the keys.No? Commented Jun 22, 2017 at 17:10
  • Sorry in last comment it's a array_unique() try it. Commented Jun 22, 2017 at 17:11
  • Possible duplicate of How to remove duplicate keys in array Commented Jun 22, 2017 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. Commented Jun 22, 2017 at 17:16
  • Sorry i think it was you actual array Commented Jun 22, 2017 at 17:17

1 Answer 1

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
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

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: 3v4l.org/cg5WC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.