【问题标题】:Create unique array as per one of the array element根据数组元素之一创建唯一数组
【发布时间】:2014-09-14 12:09:50
【问题描述】:

我在 PHP 中有以下数组。它是一个对象数组。我正在尝试从中创建一个独特的数组。

array
  0 => 
    object(ElggAnnotation)[1144]
      protected 'attributes' => 
        array
          'id' => string '21705' (length=5)
          'owner_guid' => string '40468' (length=5)
          'access_id' => string '2' (length=1)
          'time_created' => string '1406098167' (length=10)
          'enabled' => string 'yes' (length=3)
      private 'valid' (ElggExtender) => boolean false
  1 => 
    object(ElggAnnotation)[1145]
      protected 'attributes' => 
        array
          'id' => string '21706' (length=5)
          'owner_guid' => string '28715' (length=5)
          'access_id' => string '2' (length=1)
          'time_created' => string '1406098168' (length=10)
          'enabled' => string 'yes' (length=3)
          'name' => string 'scheduled' (length=9)
          'value' => string 'yes' (length=3)
          'type' => string 'annotation' (length=10)
      private 'valid' (ElggExtender) => boolean false
  2 => 
    object(ElggAnnotation)[1146]
      protected 'attributes' => 
        array
          'id' => string '21707' (length=5)
          'owner_guid' => string '40468' (length=5)
          'access_id' => string '2' (length=1)
          'time_created' => string '1406104062' (length=10)
          'enabled' => string 'yes' (length=3)           
      private 'valid' (ElggExtender) => boolean false
  3 => 
    object(ElggAnnotation)[1147]
      protected 'attributes' => 
        array
          'id' => string '21708' (length=5)
          'owner_guid' => string '28715' (length=5)
          'access_id' => string '2' (length=1)
          'time_created' => string '1406104062' (length=10)
          'enabled' => string 'yes' (length=3)              
      private 'valid' (ElggExtender) => boolean false
  4 => 
    object(ElggAnnotation)[1148]
      protected 'attributes' => 
        array
          'id' => string '21709' (length=5)
          'owner_guid' => string '40468' (length=5)
          'access_id' => string '2' (length=1)
          'time_created' => string '1406104195' (length=10)
          'enabled' => string 'yes' (length=3)              
      private 'valid' (ElggExtender) => boolean false

所以我需要根据它的元素创建一个唯一的数组 'owner_guid' ...我尝试了 array_unique 方法..但它对我不起作用...

伙计们,怎么样?有什么想法吗?

【问题讨论】:

标签: php arrays elgg


【解决方案1】:
$unique = array();

foreach($objects as $object) {
    $unique[$object->owner_guid] = $object;
}

var_dump($unique);

【讨论】:

  • @RIADev $unique 将包含每个owner_guid 1 个对象,您可以通过array_keys($unique) 获得唯一的owner_guid
  • 嘿,这可以很容易地完成......但是我的问题是通过使用 owner_guid 可以使主阵列独一无二
  • @RIADev 这就是这样做的......试试吧,如果它不起作用,请在问题中发布您的预期输出
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-02
相关资源
最近更新 更多