1

I have an array that named $keys. The $keys values are:

$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" 

I need to delete duplicated values in this array. But when I used the array_unique() function the result was:

"1/7/95" 

Why?

This is my code, I use this code to make telegram bot keyboard:

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

But in my bot keyboard I just recieve "1/7/95".

3
  • 1
    Not sure what exactly you've tried. Using $arr = array_unique($arr); will give you the output: Array ( [0] => 1/7/95 [4] => 22/7/95 [7] => 11/7/95 [11] => main page ) Commented Dec 23, 2016 at 11:04
  • Try with array_values(array_unique($array_name)). Commented Dec 23, 2016 at 11:05
  • array_unique() work fine : eval.in/703489 Commented Dec 23, 2016 at 11:06

4 Answers 4

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); ?> 
Sign up to request clarification or add additional context in comments.

Comments

1

array_unique() returns an array with all the unique values, so I'm assuming you're just using/printing the first value?

Doing

print_r(array_unique($keys); 

outputs

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

See working example: https://3v4l.org/fOm2s

The manual states

Takes an input array and returns a new array without duplicate values.

Comments

1

It is recommended to share code sample you are using. Any way here is a sample code to explain.

<?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) */ ?> 

Comments

0

For that is you want to only unique value from the table you can add group by in your query. Like:-

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.