Quantcast
Channel: Suburban Glory
Viewing all articles
Browse latest Browse all 101

Checking for unique values in a multidimensional array

$
0
0

PHP elephant

There are lots of different solutions to this problem posted on forums all over the web. But here is a very simple solution I came up with the other day.

Below was an array whose values were from a form. I wanted to make sure that the user only submitted unique data for every field.

array
  1 => 
    array
      'feedName' => string 'Name of feed here' (length=17)
      'input_gen' => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a383a22666565644e616d65223b733a323a226964223b733a393a22666565642d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32313a22546865206e616d65206f662074686520666565643a223b733a393a226d61786c656e677468223b733a333a22323030223b733a353a2276616c7565223b733a31373a224e616d65206f6620666565642068657265223b733a363a2273656c656374223b623a303b7d' (length=430)
  2 => 
    array
      'urlName' => string 'http://www.bbc.co.uk/news/world-middle-east-17323093' (length=52)
      'input_gen' => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a373a2275726c4e616d65223b733a323a226964223b733a383a2275726c2d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32303a225468652055524c206f662074686520666565643a223b733a393a226d61786c656e677468223b733a333a22333030223b733a353a2276616c7565223b733a34373a22687474703a2f2f7068702e6e65742f6d616e75616c2f656e2f66756e6374696f6e2e61727261792d706f702e706870223b733a363a2273656c656374223b623a303b7d' (length=484)
  3 => 
    array
      'urlName' => string 'http://www.bbc.co.uk/news/uk-politics-17323504' (length=46)
      'input_gen' => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a373a2275726c4e616d65223b733a323a226964223b733a383a2275726c2d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32343a225468652055524c206f662074686520666565642074776f3a223b733a393a226d61786c656e677468223b733a333a22333030223b733a353a2276616c7565223b733a38313a22687474703a2f2f737461636b6f766572666c6f772e636f6d2f7175657374696f6e732f373338393137362f636f6d706172652d6d756c746964696d656e73696f6e616c2d6172726179732d696e'... (length=560)
  4 => 
    array
      'feedName' => string 'Name of feed here too' (length=21)
      'input_gen' => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a383a22666565644e616d65223b733a323a226964223b733a393a22666565642d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32313a22546865206e616d65206f662074686520666565643a223b733a393a226d61786c656e677468223b733a333a22323030223b733a353a2276616c7565223b733a333a22594553223b733a363a2273656c656374223b623a303b7d' (length=400)
  5 => 
    array
      'urlName' => string 'http://www.bbc.co.uk/news/uk-england-tyne-17324795' (length=50)
      'input_gen' => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a373a2275726c4e616d65223b733a323a226964223b733a383a2275726c2d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32303a225468652055524c206f662074686520666565643a223b733a393a226d61786c656e677468223b733a333a22333030223b733a353a2276616c7565223b733a333a22594553223b733a363a2273656c656374223b623a303b7d' (length=394)
  6 => 
    array
      'urlName' => string 'http://www.bbc.co.uk/news/uk-17282903' (length=37)
      'input_gen' => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a373a2275726c4e616d65223b733a323a226964223b733a383a2275726c2d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32343a225468652055524c206f662074686520666565642074776f3a223b733a393a226d61786c656e677468223b733a333a22333030223b733a353a2276616c7565223b733a333a22594553223b733a363a2273656c656374223b623a303b7d' (length=402)

The first part of the code meant trimming down the multidimensional array to something more manageable like so (don't be distracted by the long strings of hex numbers - that's another story for another time):

$tmp = array();

foreach ($array as $row) {

    foreach ($row as $result) {

        $tmp[] = $result;

    } // end foreach
    
} // end foreach

So now the array looks like this:

array
  0 => string 'Name of feed here' (length=17)
  1 => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a383a22666565644e616d65223b733a323a226964223b733a393a22666565642d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32313a22546865206e616d65206f662074686520666565643a223b733a393a226d61786c656e677468223b733a333a22323030223b733a353a2276616c7565223b733a31373a224e616d65206f6620666565642068657265223b733a363a2273656c656374223b623a303b7d' (length=430)
  2 => string 'http://www.bbc.co.uk/news/world-middle-east-17323093' (length=52)
  3 => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a373a2275726c4e616d65223b733a323a226964223b733a383a2275726c2d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32303a225468652055524c206f662074686520666565643a223b733a393a226d61786c656e677468223b733a333a22333030223b733a353a2276616c7565223b733a34373a22687474703a2f2f7068702e6e65742f6d616e75616c2f656e2f66756e6374696f6e2e61727261792d706f702e706870223b733a363a2273656c656374223b623a303b7d' (length=484)
  4 => string 'http://www.bbc.co.uk/news/uk-politics-17323504' (length=46)
  5 => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a373a2275726c4e616d65223b733a323a226964223b733a383a2275726c2d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32343a225468652055524c206f662074686520666565642074776f3a223b733a393a226d61786c656e677468223b733a333a22333030223b733a353a2276616c7565223b733a38313a22687474703a2f2f737461636b6f766572666c6f772e636f6d2f7175657374696f6e732f373338393137362f636f6d706172652d6d756c746964696d656e73696f6e616c2d6172726179732d696e'... (length=560)
  6 => string 'Name of feed here too' (length=21)
  7 => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a383a22666565644e616d65223b733a323a226964223b733a393a22666565642d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32313a22546865206e616d65206f662074686520666565643a223b733a393a226d61786c656e677468223b733a333a22323030223b733a353a2276616c7565223b733a333a22594553223b733a363a2273656c656374223b623a303b7d' (length=400)
  8 => string 'http://www.bbc.co.uk/news/uk-england-tyne-17324795' (length=50)
  9 => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a373a2275726c4e616d65223b733a323a226964223b733a383a2275726c2d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32303a225468652055524c206f662074686520666565643a223b733a393a226d61786c656e677468223b733a333a22333030223b733a353a2276616c7565223b733a333a22594553223b733a363a2273656c656374223b623a303b7d' (length=394)
  10 => string 'http://www.bbc.co.uk/news/uk-17282903' (length=37)
  11 => string '613a383a7b733a343a2274797065223b733a343a2274657874223b733a343a226e616d65223b733a373a2275726c4e616d65223b733a323a226964223b733a383a2275726c2d6e616d65223b733a353a22636c617373223b623a303b733a343a2264657363223b733a32343a225468652055524c206f662074686520666565642074776f3a223b733a393a226d61786c656e677468223b733a333a22333030223b733a353a2276616c7565223b733a333a22594553223b733a363a2273656c656374223b623a303b7d' (length=402)

The magic comes in using array_unique(). If there are any values that are the same then this function will remove them. So, by comparing the number of elements in the array with and without using array_unique() we know there is repetition if there is a dissonance between the numbers. How easy is that!

The full function looks like so:

function duplicate_entries($array) {

    $tmp = array();

    foreach ($array as $row) {

        foreach ($row as $result) {

            $tmp[] = $result;

        } // end foreach

    } // end foreach

    if (count($tmp) !== count(array_unique($tmp))) {
        return FALSE;
    }

}

Viewing all articles
Browse latest Browse all 101

Latest Images

Trending Articles





Latest Images