Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于特定键的值的唯一数组_Php_Arrays - Fatal编程技术网

Php 基于特定键的值的唯一数组

Php 基于特定键的值的唯一数组,php,arrays,Php,Arrays,我有以下数组: Array ( [0] => Array ( [hotelID] => 10 [hotelcategoryID] => 12 [hotelName] => Grand Forest Metsovo [hotelShortDescription] => [hotelVisible] => 1 [roomID] => 2

我有以下数组:

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
    )

)
我有两个不同的hotelID钥匙。我只想提取hotelID在整个数组中唯一的一个元素(第一个)。我正在使用以下代码进行尝试:

$data['uniqueHotels'] = array_map('unserialize', array_unique(array_map('serialize', $hotels)));
但到目前为止没有任何运气


任何人都可以给我一个提示吗?

您只需在数组中循环,然后将它们添加到一个新数组中,由
hotelID
索引即可。这样,任何重复项都将覆盖现有值,最终每个酒店都会有一个条目:

$unique = array();

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

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

您只需在数组中循环并将它们添加到一个新数组中,该数组由
hotelID
索引。这样,任何重复项都将覆盖现有值,最终每个酒店都会有一个条目:

$unique = array();

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

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

如果要查找第一个元素:

<?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);

如果要查找第一个元素:

<?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);

按照你的想法

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

按照你的想法

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

以下是一个动态解决方案:

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

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

 $data = array_values($unique);

 return $data;
}
如何使用:
uniqueascarray($yourray,'theKey')

以下是一个动态解决方案:

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

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

 $data = array_values($unique);

 return $data;
}
如何使用:
uniqueascarray($yourray,'theKey')

您的问题不清楚。你在找什么?具有唯一hotelID的阵列?如何处理副本?当hotelID相同时,其他字段是否始终相同?(不!)是的,我只想要不同的hotelID键。第一个元素就行了。其他元素可能会释放。您的问题不清楚。你在找什么?具有唯一hotelID的阵列?如何处理副本?当hotelID相同时,其他字段是否始终相同?(不!)是的,我只想要不同的hotelID键。第一个元素就行了。其他元素可能会被释放。应接受此答案,因为他的函数返回所有数组,而不仅仅是唯一键。应接受此答案,因为他的函数返回所有数组,而不仅仅是唯一键