1

i have a new problem and my mind is burning, i have an array in php

$test = array();
    Array
    (
        [0] => Array
            (
                [account] => 14319896
                [value] => 725.57
                [id] => 280
            )

        [1] => Array
            (
                [account] => 163157
                [value] => -723.57
                [id] => 283
            )

        [2] => Array
            (
                [account] => 163157
                [value] => 723.57
                [id] => 284
            )

        [3] => Array
            (
                [account] => 161817
                [value] => -723.57
                [id] => 285
            )

    )

i need the accounts, they are more than one in this array, in this example i need $test[1][id] and $test[2][id]

have you an idea? i have no idea more at this time.

Thanks for your help.

2
  • Can you be more explicit? you only want the ids of account that are duplicated? Commented Dec 27, 2012 at 20:21
  • yes i need the id. in this example i need $var1 = '283' from $test[1][id] and $var2 = '284' from $test[2][id] Commented Dec 27, 2012 at 20:23

2 Answers 2

4

Use the account number as key in a new array, count each entry and then take the items with a count > 1

$dupes = array();
foreach($array as $account) {
  ++$dupes[$account['account']];
}

$dupes = array_filter($dupes, function($count) { return $count > 1; });

Editing to answer the comments below the question …

If you want the IDs (or keys) of the duplicates, do not store the count directly, but use another array instead.

$dupes = array();
foreach($array as $key => $account) {
  if(!array_key_exists($account, $dupes))
    $dupes[$account['account']] = array();
  $dupes[$account['account']][] = $account['id']; // or: = $key
}

$dupes = array_filter($dupes, function($ids) { return count($ids) > 1; });
Sign up to request clarification or add additional context in comments.

2 Comments

i try this but, this if(!array_key_exists($account)) will not work missing the key do check array_key_exists, and $dupes are empty.
@JasperM: whoops, missed the second parameter to array_key_exists. But it should even work without the if-condition, because PHP will simply create the array if it does not exist yet when using the [] syntax.
0

You should change your architecture to have a associative array where keys are account number organized like that :

 Array
(
    [14319896] => Array
        (
            [0]=>Array(
                [value] => 725.57
                [id] => 280
            )
        )

    [163157] => Array
        (
            [0]=>Array(
                [value] => -723.57
                [id] => 283
             )
            [1]=>Array(
                [value] => 723.57
                [id] => 284
            )
        )

    [161817] => Array
        (
            [0]=>Array(
                [value] => -723.57
                [id] => 285
            )
        )

)


$dupes = array();
foreach($array as $key => $account) {

  $dupes[$account['account']][] = array($account['id'],$account['value']); 
}

Whith that architecture you can get everything you want thanks to array_filter :

$dups = array_filter($dupes, function($account){ return count($account)>1;});

4 Comments

looks good but I do not know beforehand which occurs more than once account, the data come from a mysql db,i can not change the database unfortunately
If the data comes from a database, then why don't you filter beforehand with SQL?
i try this but i can only filter on one duplicate but i have 2 colums there have dupes. account and billno. the select statement are so confuse that i try this in php with array
ok so just take the knittl code without the array_key_exists verification to transform your array. it's on my edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.