使多维数组成为唯一的php [英] Make multidimensional array unique php

查看:77
本文介绍了使多维数组成为唯一的php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数组如下:

数组(

    [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

        ) 

)

我有一系列产品需要确保其独特性.需要通过ID使此数组唯一.这些数组是通过推入值生成的.

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.

我现在试图解决这个问题已有一个多星期了,但是我没有使它起作用.我知道这应该很容易...但是无论如何-我不明白:D

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

推荐答案

可以尝试一下吗,

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

输出:

   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
        )

   )

这篇关于使多维数组成为唯一的php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆