Jump to content

Archived

This topic is now archived and is closed to further replies.

tuna

array_unique help

Recommended Posts

if i have an array like this

[code]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"
  }
}[/code]

How would i remove the duplicates using the array_unique function ?

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

Share this post


Link to post
Share on other sites
the array(x) is the count (how many items in that array)

Share this post


Link to post
Share on other sites
Try
[code]<?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>';
?>[/code]

This has been tested and works.

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.