Jump to content


Photo

array_unique help


  • Please log in to reply
3 replies to this topic

#1 tuna

tuna
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 April 2006 - 06:08 AM

if i have an array like this

array(6) {
  [5]=>
  array(1) {
    [0]=>
    string(8) "20060411"
  }
  [4]=>
  array(1) {
    [0]=>
    string(8) "20060407"
  }
  [3]=>
  array(1) {
    [0]=>
    string(8) "20060411"
  }
  [2]=>
  array(1) {
    [0]=>
    string(8) "20060411"
  }
  [1]=>
  array(1) {
    [0]=>
    string(8) "20060407"
  }
  [0]=>
  array(1) {
    [0]=>
    string(8) "20060407"
  }
}

How would i remove the duplicates using the array_unique function ?

i've tried many ways, and still cant seem to get it
thanks

#2 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 11 April 2006 - 10:02 AM

array(1)=array_unique(array(1)); doesn't work?
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#3 tuna

tuna
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 April 2006 - 11:02 AM

the array(x) is the count (how many items in that array)

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 April 2006 - 11:05 AM

Try
<?php
$curr_array = array (
  5=>
  array (
    0=>"20060411"
  ),
  4=>
  array (
    0=>"20060407"
  ),
  3=>
  array(
    0=>"20060411"
  ),
  2=>
  array(
    0=>"20060411"
  ),
  1=>
  array(
    0=>"20060407"
  ),
  0=>
  array(
    0=>"20060407"
  )
);
echo '<pre>' . var_export($curr_array,true) . '</pre>';
$new_array = array();
foreach($curr_array as $k => $v)
    $new_array[$k] = serialize($curr_array[$k]);
$curr_array = array();
foreach(array_unique($new_array) as $k => $v)
    $curr_array[$k] = unserialize($new_array[$k]);
echo '<pre>' . var_export($curr_array,true) . '</pre>';
?>

This has been tested and works.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users