简体   繁体   English

PHP array_unique没有索引

[英]PHP array_unique without index

I've been trying to find something similar to array_unique() to the extent or removing all the duplicates from an array. 我一直在尝试找到与array_unique()类似的东西,或者从数组中删除所有重复项。 However in this particular case I need a comma spaced array as the result rather than an key value pair. 但是在这种特殊情况下,我需要一个逗号分隔的数组作为结果,而不是一个键值对。 What I'd like to do is avoid running this through a loop to do it, if I can. 我想做的是,如果可以的话,请避免通过循环来运行它。

So if theres something like array_unique() or something I can apply to my array there after to strip the numeric index out then I am all ears for suggestions. 因此,如果有诸如array_unique()东西,或者可以在array_unique()数字索引之后将其应用于我的数组,那么我会全力以赴。

Currently I am building the initial array through a loop, this look cycles over 5 to 150,000 entries of which one particular entry can have any of about 1,000 possible 3 dot style versioning numbers. 目前,我正在通过循环构建初始数组,此外观在5到150,000个条目之间循环,其中一个特定条目可以具有大约1,000个可能的3点样式版本号。 This is what I am building this array of. 这就是我正在构建的数组。 So I have many duplicates put into the array 所以我把很多重复的东西放进了数组

example: 例:

$myarray = array();
foreach($obj as $prod)
{
   //cycling over the master loop adding all my other stuff and building it out..
   $myarray[] = $prod['version'];
}

which gives me quite a large array in the format I'd like just 这以我想要的格式给了我很大的数组

Array(
   "1.0.0",
   "1.0.1",
   "3.0.2220",
   "2.0.0",
   "2.0.1",
   "1.8.11",
   "3.0.2220",
   "3.0.2220",
   "2.0.0",
   "2.0.0",
   "2.0.0"
)

Where afte the loop is complete I do. 循环完成后,我会做。

$myarray = array_unique($myarray);

Which leaves me with an array like 这给我留下了像这样的数组

Array(
  [0] = "1.0.0",
  [1] = "1.0.1",
  [2] = "3.0.2220",
  [3] = "2.0.0",
  [4] = "2.0.1",
  [5] = "1.8.11",
)

Which is not desired, what is desired as the end result is.. 最终结果是不希望的。

Array(
   "1.0.0",
   "1.0.1",
   "3.0.2220",
   "2.0.0",
   "2.0.1",
   "1.8.11",
)

Where I can then sort it out to list in order earliest to latest versions.. 然后我可以在其中进行排序以按最早的最新版本列出。

This: 这个:

Array(
  [0] = "1.0.0",
  [1] = "1.0.1",
  [2] = "3.0.2220",
  [3] = "2.0.0",
  [4] = "2.0.1",
  [5] = "1.8.11",
)

and this: 和这个:

Array(
   "1.0.0",
   "1.0.1",
   "3.0.2220",
   "2.0.0",
   "2.0.1",
   "1.8.11",
)

are the same thing, in PHP all arrays have an index, in the last example it's just that is not printing them, but they are there, and if you have not explicitly specified the indexes, they are just sequencial integer numbers starting from zero, like the first example. 同一件事,在PHP中,所有数组都有一个索引,在上一个示例中,只是不打印它们而已,但是它们在那里,并且如果您未明确指定索引,它们只是从零开始的顺序整数,像第一个例子一样。

In your last example, the one which apparently has no indexes, if we call it $array we can do the following to print its indexes: 在您的最后一个示例中,该示例显然没有索引,如果将其称为$array则可以执行以下操作以打印其索引:

foreach ($array as $key => $value) {
   echo $key; //this will print the indexes of the array
}

You are wanting an array without keys? 您想要一个没有键的数组吗? Every array has keys. 每个数组都有键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM