1

I have an array of objects

[new BalanceSheet('2018', '356743', '-56913', '189632', '122733'),
 new BalanceSheet('2018', '4562343', '236913', '198732', '6552733'),
 new BalanceSheet('2017', '2234643', '55913', '19435342', '855342733')
]

And i need to throw an exception when the first key in object are duplicate another -> 2018 === 2018

I understand how i can to get the key

  class BalanceSheet  {
   public function __construct(string $year, ...) {
        $this->year = $year;    
        ...
    }
  public function getYear() {
        return $this->year;
    }
private function isDuplicateData($sheets) {
        foreach ($sheets as $key => $sheet) {
            echo $key . ' year = ' . $sheet->getYear();
        }
    }

but i don't understand what the easiest way to compare keys in isDuplicateData

1
  • If your array is sorted, you could perform a binary search. Depends on your use case. Commented May 12, 2020 at 5:04

4 Answers 4

1

If all you need to do is throw an exception, then maybe create an array of the keys and apply array_unique().

    private function isDuplicateData($sheets) {
        $keys = [];
        foreach ($sheets as $key => $sheet) {
             $keys[]=$key;
        }
        if (count($keys) != count(array_unique($keys)){
            //throw exception
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

How about using php array_filter:

$objects = array(
    new BalanceSheet('2018', '356743', '-56913', '189632', '122733'),
    new BalanceSheet('2018', '4562343', '236913', '198732', '6552733'),
    new BalanceSheet('2017', '2234643', '55913', '19435342', '855342733'),
);
$known = array();
$filtered = array_filter($objects, function ($val) use (&$known) {
    // In your case the key would be years
    $unique = !in_array($val->years, $known);
    $known[] = $val->years;
    return $unique;
});

Comments

0

You can compare length of array and length of array_diff.

count($arrayA) == count(array_diff($arrayA,$arrayB));

Comments

0

If you want to delete duplicates you can use the PHP function array_unique, this will check for the duplicates and delete them:

array_unique ( $sheets );

If you want to throw an exception only, you can extract and add the years to another array then check if there are any duplicates:

private function isDuplicateData($sheets) {
$isdublicate = array();
foreach ($sheets as $key => $sheet) {
            echo $key . ' year = ' . $sheet->getYear();

$isdublicate[] = $sheet->getYear();
}
for ($i = 0; $i <  count($isdublicate); $i++) 
    { 
        if ($isdublicate[abs($isdublicate[$i])] >= 0) 
            $isdublicate[abs($isdublicate[$i])] = -$isdublicate[abs($isdublicate[$i])]; 
        else
            //Here throw the exception 
    } 
}

The above for loop logic is here.

Comments

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.