我的数组如下所示:
大批 (
[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 使该数组唯一。这些数组是通过推送值生成的。
我已经尝试解决这个问题一个多星期了,但我没有成功。我知道这应该很容易......但无论如何 - 我不明白: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);
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
)
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)