{"id":602,"date":"2024-01-01T18:01:01","date_gmt":"2024-01-01T10:01:01","guid":{"rendered":""},"modified":"2024-01-01T18:01:01","modified_gmt":"2024-01-01T10:01:01","slug":"php\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/602.html","title":{"rendered":"php\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5"},"content":{"rendered":"
<?php
$aa = array(\"apple\", \"banana\", \"pear\", \"apple\", \"wail\", \"watermalon\");
$bb = array_unique($aa);
print_r($bb);<\/p>\n
<\/p>\n
$result = array(
0=>array('a'=>1,'b'=>'Hello'),
1=>array('a'=>1,'b'=>'other'),
2=>array('a'=>1,'b'=>'other'),
);<\/p>\n
\u5904\u7406\u6210<\/p>\n
$result = array(
0=>array('a'=>1,'b'=>'Hello'),
1=>array('a'=>1,'b'=>'other')
);<\/p>\n
\u4f7f\u7528\u65b9\u6cd5<\/p>\n
array_unique($result, SORT_REGULAR);<\/p>\n
\/**
* @param array $arr [description]
* @return [type] [description]
*\/
static function super_unique($array, $recursion = false){
\n
\/\/ \u5e8f\u5217\u5316\u6570\u7ec4\u5143\u7d20,\u53bb\u9664\u91cd\u590d
$result = array_map('unserialize', array_unique(array_map('serialize', $array)));
\/\/ \u9012\u5f52\u8c03\u7528
if ($recursion) {
\n
foreach ($result as $key => $value) {
\n
if (is_array($value)) {
\n
$result[ $key ] = super_unique($value);
}
}
}
return $result;
}<\/p>\n
<\/p>\n
<?php
$str = '{
\n
\"status\": 0,
\"result\": [
{
\n
\"hostName\": \"\u6768\u65b0\",
\"pcode2\": \"41675441\",
\"email\": \"xin.yang@xx.cn\",
\"selfName\": \"\u90b5\u5175\"
},
{
\n
\"hostName\": \"\u6768\u65b0\",
\"pcode2\": \"41675441\",
\"email\": \"xin.yang@xx.cn\",
\"selfName\": \"\u90b5\u5175\"
},
{
\n
\"hostName\": \"\u4e2d\u4fe1\u56fd\u5b89\u79d1\u6280\u63a7\u80a1\u6709\u9650\u516c\u53f8\",
\"pcode2\": \"9730284461\",
\"email\": \"liuxiao@gakj.xx.com\",
\"selfName\": \"\u90b5\u5175\"
},
{
\n
\"hostName\": \"\u4e2d\u4fe1\u56fd\u5b89\u79d1\u6280\u63a7\u80a1\u6709\u9650\u516c\u53f8\",
\"pcode2\": \"9730284461\",
\"email\": \"liuxiao@gakj.xx.com\",
\"selfName\": \"\u90b5\u5175\"
},
{
\n
\"hostName\": \"\u5218\u60e0\",
\"pcode2\": \"64068956\",
\"email\": \"huijie.liu@xx.cn\",
\"selfName\": \"\u90b5\u5175\"
},
{
\n
\"hostName\": \"\u5218\u60e0\",
\"pcode2\": \"64068956\",
\"email\": \"huijie.liu@xx.cn\",
\"selfName\": \"\u90b5\u5175\"
},
{
\n
\"hostName\": \"\u5218\u51ef\u5357\",
\"pcode2\": \"14818360\",
\"email\": \"kainan.liu_3@xx.cn\",
\"selfName\": \"\u90b5\u5175\"
},
{
\n
\"hostName\": \"\u5218\u51ef\u5357\",
\"pcode2\": \"14818360\",
\"email\": \"kainan.liu_3@xx.cn\",
\"selfName\": \"\u90b5\u5175\"
},
{
\n
\"hostName\": \"\u5b8b\u7eac\u5947\",
\"pcode2\": \"62947142\",
\"email\": \"weiqi.song@xx.cn\",
\"selfName\": \"\u90b5\u5175\"
},
{
\n
\"hostName\": \"\u5b8b\u7eac\u5947\",
\"pcode2\": \"62947142\",
\"email\": \"weiqi.song@xx.cn\",
\"selfName\": \"\u90b5\u5175\"
}
],
\"content_type\": \"application\/json\"
}';
$res = json_decode($str,true);
echo 'num: '.count($res['result']);
print_r($res['result']);
echo \"<\/br><\/br><\/br>\";
$rrr = $res['result'];
$serializeArrs = array_map('serialize',$rrr);
$uniqueArrs = array_unique($serializeArrs);
$unserializeArrs = array_map('unserialize',$uniqueArrs);
print_r($unserializeArrs);
echo \"<\/br><\/br><\/br>\";
echo json_encode($unserializeArrs);
exit;<\/p>\n
<\/p>\n
<?php
function assoc_unique($arr, $key) {
\n
$tmp_arr = array();
foreach ($arr as $k => $v) {
\n
if (in_array($v[$key], $tmp_arr)) {\/\/\u641c\u7d22$v[$key]\u662f\u5426\u5728$tmp_arr\u6570\u7ec4\u4e2d\u5b58\u5728\uff0c\u82e5\u5b58\u5728\u8fd4\u56detrue
unset($arr[$k]);
} else {
\n
$tmp_arr[] = $v[$key];
}
}
sort($arr); \/\/sort\u51fd\u6570\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f
return $arr;
}$aa = array(
array('id' => 123, 'name' => '\u5f20\u4e09'),
array('id' => 123, 'name' => '\u674e\u56db'),
array('id' => 124, 'name' => '\u738b\u4e94'),
array('id' => 125, 'name' => '\u8d75\u516d'),
array('id' => 126, 'name' => '\u8d75\u516d')
);
$key = 'id';
assoc_unique(&$aa, $key);
print_r($aa);<\/p>\n
<\/p>\n
<?php
function array_unique_fb($array2D) {
\n
foreach ($array2D as $v) {
\n
$v = join(\",\", $v); \/\/\u964d\u7ef4,\u4e5f\u53ef\u4ee5\u7528implode,\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u7528\u9017\u53f7\u8fde\u63a5\u7684\u5b57\u7b26\u4e32
$temp[] = $v;
}
$temp = array_unique($temp);\/\/\u53bb\u6389\u91cd\u590d\u7684\u5b57\u7b26\u4e32,\u4e5f\u5c31\u662f\u91cd\u590d\u7684\u4e00\u7ef4\u6570\u7ec4
foreach ($temp as $k => $v) {
\n
$temp[$k] = explode(\",\", $v);\/\/\u518d\u5c06\u62c6\u5f00\u7684\u6570\u7ec4\u91cd\u65b0\u7ec4\u88c5
}
return $temp;
}
$aa = array(
array('id' => 123, 'name' => '\u5f20\u4e09'),
array('id' => 123, 'name' => '\u674e\u56db'),
array('id' => 124, 'name' => '\u738b\u4e94'),
array('id' => 123, 'name' => '\u674e\u56db'),
array('id' => 126, 'name' => '\u8d75\u516d')
);
$bb = array_unique_fb($aa);
print_r($bb)<\/p>\n
<\/p>\n
\/\/\u5047\u5982 \u6570\u636e\u662f\u8fd9\u6837\u7684 $arr=[ [ 'goods_name'=>'xxx', 'goods_id'=>111, ], [
'goods_name'=>'xxx',
'goods_id'=>123,
],
[
'goods_name'=>'xxx',
'goods_id'=>'123'
],
];
function array_unset_tt($arr,$key='goods_id'){
\n
$res = array();
foreach ($arr as $value) {
\n
\/\/\u67e5\u770b\u6709\u6ca1\u6709\u91cd\u590d\u9879
if(isset($res[$value[$key]])){
\n
unset($value[$key]); \/\/\u6709\uff1a\u9500\u6bc1
}else{
\n
$res[$value[$key]] = $value;
}
}
return $res;
}
\u8c03\u7528\u65b9\u6cd5\u5904\u7406\u540e,\u5c31\u628a\u91cd\u590d\u7684goods_id\u7684\u6570\u7ec4\u8fc7\u6ee4\uff0c\u53ea\u5269\u4e0b\u4e00\u4e2a\uff01\uff01\uff01\u5f00\u5fc3\u5417?<\/p>\n
<\/p>\n
<\/p>\n
<\/p>\n
<\/p>\n
<\/p>\n","protected":false},"excerpt":{"rendered":"php\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u4e00\u7ef4\u6570\u7ec4\u7684\u91cd\u590d\u9879?php$aa=array(\"apple\",\"banana\",\"pear\",\"apple\",\"wail\",\"watermalon\");$bb=array_unique($a...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/602"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=602"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/602\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}