user1hjgjhgjhggjhg
user1hjgjhgjhggjhg

Reputation: 1327

Fetch only unique data from the array

I have data in an array. The data looks like this

Array
(
    [1-2] => Array
        (
            [-LTTzVgH1H4-mCg9vUPT] => Array
                (
                    [chat_id] => -LTTzVgH1H4-mCg9vUPT

                    [receiver_id] => 10215158688973543
                    [sender_id] => 1934076580016543

                )

        )
)

So Basically It has been structured like this

1-2
3-4
5-6
2-1
6-5

I want to fetch only those keys which are unique. Meaning I want to get the result like this

1-2
3-4
5-6

How can I fetch only unique keys data?

Note: Both 1-2 or 2-1 has the same data

This is what I've tried

foreach($json_data as $key=>$value){
   echo $key.'<br>';


}

This gives me all the keys atm

Upvotes: 1

Views: 55

Answers (1)

Jeto
Jeto

Reputation: 14927

You could "reorder" the keys, then remove duplicate ones:

$unique_keys = array_unique(array_map(function ($key) {
  $parts = explode('-', $key);
  sort($parts);
  return implode('-', $parts);
}, array_keys($array)));

Demo here: https://3v4l.org/Fr1qD

Upvotes: 4

Related Questions