user2417624
user2417624

Reputation: 693

unique array on base on value of specific key

I have the following array:

Array
(
    [0] => Array
    (
        [hotelID] => 10
        [hotelcategoryID] => 12
        [hotelName] => Grand Forest Metsovo
        [hotelShortDescription] => 
        [hotelVisible] => 1
        [roomID] => 2
    )

    [1] => Array
    (
        [hotelID] => 10
        [hotelcategoryID] => 12
        [hotelName] => Grand Forest Metsovo
        [hotelShortDescription] => 
        [hotelVisible] => 1
        [roomID] => 3
    )

    [2] => Array
    (
        [hotelID] => 10
        [hotelcategoryID] => 12
        [hotelName] => Grand Forest Metsovo
        [hotelShortDescription] => 
        [hotelVisible] => 1
        [roomID] => 4
    )

    [3] => Array
    (
        [hotelID] => 14
        [hotelcategoryID] => 7
        [hotelName] => Hotel Metropolis
        [hotelShortDescription] => 
        [hotelVisible] => 1
        [roomID] => 23
    )

    [4] => Array
    (
        [hotelID] => 14
        [hotelcategoryID] => 7
        [hotelName] => Hotel Metropolis
        [hotelShortDescription] => 
        [hotelVisible] => 1
        [roomID] => 24
    )

)

I have two different hotelID keys. I would like to extract only one element (the first one) where the hotelID is unique in whole array. I am trying with following code:

$data['uniqueHotels'] = array_map('unserialize', array_unique(array_map('serialize', $hotels)));

but without any luck so far.

Anyone can give me a hint?

Upvotes: 3

Views: 10749

Answers (4)

Ousama Y.
Ousama Y.

Reputation: 21

Here is a dynmaic solution:

function uniqueAssocArray($array, $uniqueKey){
 $unique = array();

 foreach ($array as $value){
  $unique[$value[$uniqueKey]] = $value;
 }

 $data = array_values($unique);

 return $data;
}

How to use: uniqueAssocArray($yourArray, 'theKey');

Upvotes: 2

mgamba
mgamba

Reputation: 1199

along the lines of what you're trying,

array_unique(array_map(function($hotel) { return $hotel['hotelID']; }, $array))

Upvotes: 1

Ahsan
Ahsan

Reputation: 4154

If looking for the first element:

<?php

$hotels = array(
  array(
    'id' => 1,
    'hotelID' => 10
  ),
  array(
    'id' => 2,
    'hotelID' => 10,
  ),
  array(
    'id' => 3,
    'hotelID' => 20,
  ),
  array(
    'id' => 4,
    'hotelID' => 20,
  ),
);


function getUniqueHotels($hotels) {
  $uniqueHotels = array();

  foreach($hotels as $hotel) {
    $niddle = $hotel['hotelID'];
    if(array_key_exists($niddle, $uniqueHotels)) continue;
    $uniqueHotels[$niddle] = $hotel;
  }

  return $uniqueHotels;
}

$unique_hotels = getUniqueHotels($hotels);
print_r($unique_hotels);

results in:

Array
(
    [10] => Array
        (
            [id] => 1
            [hotelID] => 10
        )

    [20] => Array
        (
            [id] => 3
            [hotelID] => 20
        )

)

Upvotes: 4

Rhumborl
Rhumborl

Reputation: 16609

You could simply loop through the array and add them to a new array, indexed by hotelID. This way any duplicates will just overwrite the existing value and you end up with one entry per hotel:

$unique = array();

foreach ($hotels as $value)
{
    $unique[$value['hotelID']] = $value;
}

$data['uniqueHotels'] = array_values($unique);

Upvotes: 3

Related Questions