删除php数组中的重复值

时间:2016-12-23 10:58:44

标签: php arrays duplicates

我有一个名为$keys的数组。 $keys值为:

$keys[0] = "1/7/95"
$keys[1] = "1/7/95"
$keys[2] = "1/7/95"
$keys[3] = "1/7/95"
$keys[4] = "22/7/95"
$keys[5] = "22/7/95"
$keys[6] = "22/7/95"
$keys[7] = "11/7/95"
$keys[8] = "11/7/95"
$keys[9] = "11/7/95"
$keys[10] = "11/7/95"
$keys[11] = "main page"

我需要删除此数组中的重复值。但是当我使用array_unique()函数时,结果是:

"1/7/95"

为什么?

这是我的代码,我使用这段代码制作电报机器人键盘:

$list_product = mysql_query("SELECT * FROM `cart` WHERE `product` = '".$item_group['id']."' AND `status`='0' ");
while($item_type = mysql_fetch_array($list_product))
{
    $keys[$j] = array($item_type['detail']);
    $j++;
}

$keys[$j] = array('','main page'."\xF0\x9F\x94\x99");
$keys= array_unique($keys,SORT_NUMERIC);
$replyMarkup = array(
    'keyboard' => $keys
);

$list_setting = mysql_query("SELECT * FROM `setting` ");
$item_setting = mysql_fetch_array($list_setting);

$encodedMarkup = json_encode($replyMarkup);
$url = 'https://api.telegram.org/bot'.$item_setting['token'].'/sendMessage';

$ch = curl_init( );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, "text=".$text_reply."&chat_id=".$user_id."&reply_markup=" .$encodedMarkup);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 500 );
$agent = $_SERVER["HTTP_USER_AGENT"];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$check = curl_exec( $ch );

echo('OK!');

但在我的机器人键盘中,我只收到"1/7/95"

4 个答案:

答案 0 :(得分:1)

<?php
$keys = array();
$keys[0] = "1/7/95";
$keys[1] = "1/7/95";
$keys[2] = "1/7/95";
$keys[3] = "1/7/95";
$keys[4] = "22/7/95";
$keys[5] = "22/7/95";
$keys[6] = "22/7/95";
$keys[7] = "11/7/95";
$keys[8] = "11/7/95";
$keys[9] = "11/7/95";
$keys[10] = "11/7/95";
$keys[11] = "main page";
$keys = array_unique($keys);
print_r($keys);
?>

答案 1 :(得分:1)

array_unique()返回一个包含所有唯一值的数组,所以我假设你只是使用/打印第一个值?

print_r(array_unique($keys);

输出

Array
(
    [0] => 1/7/95
    [4] => 22/7/95
    [7] => 11/7/95
    [11] => main page
)

参见工作示例:https://3v4l.org/fOm2s

The manual

  

采用输入数组并返回新数组,没有重复值。

答案 2 :(得分:1)

建议您共享正在使用的代码示例。这里的任何方式都是一个示例代码来解释。

<?php    
$keys    = array();
$keys[0] = "1/7/95";
$keys[1] = "1/7/95";
$keys[2] = "1/7/95";
$keys[3] = "1/7/95";
$keys[4] = "22/7/95";
$keys[5] = "22/7/95";
$keys[6] = "22/7/95";
$keys[7] = "11/7/95";
$keys[8] = "11/7/95";
$keys[9] = "11/7/95";
$keys[10] = "11/7/95";
$keys[11] = "main page";

$uniq_keys    = array_unique($keys);
$uniq = array_keys(array_flip($keys));

var_dump($uniq_keys);
/*
array (size=4)
  0 => string '1/7/95' (length=6)
  4 => string '22/7/95' (length=7)
  7 => string '11/7/95' (length=7)
  11 => string 'main page' (length=9)
*/
var_dump($uniq);
/*
array (size=4)
  0 => string '1/7/95' (length=6)
  1 => string '22/7/95' (length=7)
  2 => string '11/7/95' (length=7)
  3 => string 'main page' (length=9)
*/
?>

答案 3 :(得分:0)

为此,您只希望在查询中可以添加分组的表中的唯一值。如: -

"SELECT * FROM `cart` WHERE `product` = '".$item_group['id']."' AND `status`='0'  GROUP BY DATE";