Make multidimensional array unique php

风流意气都作罢 提交于 2020-01-30 08:09:08

问题


My array looks like this:

Array (

    [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

        )
   [3] => stdClass Object
        (
            [id] => 222
            [user_id] => 1
            [name] => Darling New Bathtub by Duravit - $6300

        ) 

)

I have an array of products that I need to make sure are unique. Need to make this array unique by id. These array are generated by pushing value.

I'm trying to solve this for more than a week now, but I dont get it to work. I know it should be easy...but anyway - I don't get it :D


回答1:


Can you try this,

$input = array ('1' => array(
            'id' => 225,
            'user_id' => 1,
            'name' => 'Blue Quilted Leather Jacket by Minusey - $499'

        ),
        '2' => array(
                'id' => 222,
                'user_id' => 1,
                'name' => 'Darling New Bathtub by Duravit - $6300'

        ),
        '3' => array(
                'id' => 222,
                'user_id' => 1,
                'name' => 'Darling New Bathtub by Duravit - $6300'

        )                   
  );    

    $UniqueArray = array();
    foreach($input as $key=>$value){   // rebuild your array    
        //$id = $value['id']; //build array with unique key value
        $id = $value->id; //object
        $UniqueArray[$id] = $value;
    }

   print_r($UniqueArray);

output:

   Array
   (
        [225] => Array
        (
                [id] => 225
                [user_id] => 1
                [name] => Blue Quilted Leather Jacket by Minusey - $499
        )

        [222] => Array
        (
                [id] => 222
                [user_id] => 1
                [name] => Darling New Bathtub by Duravit - $6300
        )

   )



回答2:


try this simple code

$arr_new = array();
$arr_temp_ids = array();
foreach($your_array as $key=>$arr_obj)
{
    $arr_val = get_object_vars($arr_obj);
    if(!isset($arr_temp_ids[$arr_val['id']]))
    {
        $arr_new[] = $arr_obj;
        $arr_temp_ids[$arr_val['id']] = true;
    }
}
var_dump($arr_new); // resultant array



回答3:


The basic idea is to loop through the products and save which you encountered. If a product has already been found, just skip it.

<?php
function array_unique_by_key($array, $key = 'id') {
    $found = array(); // Encountered IDs
    $out = array(); // Output array

    foreach ($array as $value) {
        if (!array_key_exists($key, $value)) throw new Exception('Can\'t find key "' . $key . '"');
        $id = $value[$key];

        // If already encountered, skip
        if (in_array($id, $found)) continue;

        // Otherwise, add to found values and to output array
        $found[] = $id;
        $out[] = $value;
    }

    return $out;
}

Usage:

$unique_products = array_unique_by_key($your_products, 'id');


来源:https://stackoverflow.com/questions/25091999/make-multidimensional-array-unique-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!