使数组在多维数组php编码器中唯一



我有这个数组

阵列(

[0] => Array
    (
        [0] => stdClass Object
            (
                [id] => 226
                [user_id] => 1
                [name] => Eden Corner Tub by Glass - $2099
            )
        [1] => stdClass Object
            (
                [id] => 225
                [user_id] => 1
                [name] => Blue Quilted Leather Jacket by Minusey - $499
            )
        [2] => stdClass Object
            (
                [id] => 222
                [user_id] => 1
                [name] => Darling New Bathtub by Duravit - $6300
            )

    )
[1] => Array
    (
        [0] => stdClass Object
            (
                [id] => 226
                [user_id] => 1
                [name] => Eden Corner Tub by Glass - $2099
            )
        [1] => stdClass Object
            (
                [id] => 229
                [user_id] => 1
                [name] => Batman Tumbler Golf Cart - $50000
            )
        [2] => stdClass Object
            (
                [id] => 228
                [user_id] => 1
                [name] => Swirlio Frozen Fruit Dessert Maker - $60
            )
    )    )

我有一个产品数组,我需要确保它们是唯一的。需要使这个数组通过id唯一。这些数组通过push value生成。

我试着解决这个问题已经一个多星期了,但我没有得到它的工作。我知道这应该很容易……D

试试这个:

$array = array(
    0 =>  array(  
        "name"   => "test",  
        "id"    =>  4  
    ),  
    1   =>  array(  
        "name" =>  "test2",  
        "id" => 152  
    ), 
    2   =>  array(  
        "name" =>  "test2",  
        "id" => 152  
    )
 );
$newArray = array();
foreach($array as $value) {
    $newArray[$value['id']]['name'] = $value['name'];
    $newArray[$value['id']]['id'] = $value['id']; 
}
foreach($array as $key=>$inner_array)
{
    foreach($array as $key_again=>$array_again)
    {
        if($key != $key_again)
        {
            if($inner_array['name'] != $array_again['name'] AND $inner_array['id'] != $array_again['id'])
            {
                //its okay
            }
            else
            {
                unset($array[$key]);
            }
        }
    }
}

不确定数组是如何生成的,但是,如果可以更改,可以直接将数组键设置为id并检查id是否已经设置。

否则,您可以执行以下操作:

$unique = array();
foreach( $array as $values ) {
    if( ! isset( $unique[$values['id']] ) ) {
        $unique[$values['id']] = $values;
    }
}

这将使您的数组唯一:

$array = array(
  0 =>  array(  
    "name"   => "test",  
    "id"    =>  4  
  ),  
  1   =>  array(  
    "name" =>  "test2",  
    "id" => 152  
  ), 
  2   =>  array(  
    "name" =>  "test2",  
    "id" => 152  
  ) );
$result = array();
$index = array();
foreach($array as $i => $elem) {
  if(!isset($index[$elem['id']])) {
    $result[$i] = $elem;
    $index[$elem['id']] = 1;
  }
}
echo var_export($result);
输出:

array (
  0 => 
  array (
    'name' => 'test',
    'id' => 4,
  ),
  1 => 
  array (
    'name' => 'test2',
    'id' => 152,
  ),
)

可以。它可以被认为比for循环更干净,但我不确定性能。

$array = [
    [ "name" => "test", "id" => 4 ],
    [ "name" => "test2", "id" => 152 ], 
    [ "name" => "test2", "id" => 152 ]
];
array_walk($array, function(&$item, $idx) use($array){
    $matches = array_slice(array_keys($array, $item), 1);
    if (in_array($idx, $matches)) {
        $item = false;
    }
});
$array = array_filter($array);

编辑由于您更新了要使用的数据集,您需要将其平坦1级:

$array = call_user_func_array('array_merge', $array);

相关内容

  • 没有找到相关文章

最新更新