从数组中删除重复的键

时间:2017-06-22 17:07:16

标签: php arrays

我得到阵列的奇怪行为。我得到一个重复键的数组。我检查这是由于密钥的数据类型。我怎样才能让它与众不同。这是数组:

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
        )

)

我已尝试array_key_exists()功能检查:

$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);
        }
    }

但没有效果。我也试图改变数据类型。就像这个

$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);

我知道这是由于保存的数组中的键是字符串而新键是数字的问题。如何将它们转换为相同的类型。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我已经这样做但我不确定这是否是正确的方法。如果有更好的方法,请提出您的建议。 我更改了数据类型并创建了一个新数组。它删除了重复的值。

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

现在这是输出

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
        )

)