php 关联数组的前两个元素

发布于 2024-10-07 14:37:14 字数 584 浏览 4 评论 0原文

我已经实现了最终目标,但也许有一种更优雅的方法来实现这一目标。

如果我有一个这样的数组:

$data = array(
    'wood' => 2,
    'metal' => 5,
    'plastic' => 3, 
);

我想从 $data 中获取前 2 个键/值对(即金属:5 和塑料:3)。这是我想出的:

arsort($data);       //put values in order
reset($data);           //set pointer to first element
$first = each($data);    //assign first element to $first
array_shift($data); //remove first element from array
reset($data);           //set pointer to the new first element
$second = each($data); //assign the new first element to $second

I've accomplished the end goal for this already, but maybe there is a more elegant way of accomplishing this.

If I have an array like such:

$data = array(
    'wood' => 2,
    'metal' => 5,
    'plastic' => 3, 
);

I want to get the top 2 key/value pairs from $data (ie metal:5 & plastic:3). Here's what I came up with:

arsort($data);       //put values in order
reset($data);           //set pointer to first element
$first = each($data);    //assign first element to $first
array_shift($data); //remove first element from array
reset($data);           //set pointer to the new first element
$second = each($data); //assign the new first element to $second

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不一样的天空 2024-10-14 14:37:14

为了同时获得键和值,我会这样做:

arsort($data);
$result = array_slice($data,0,2);

For getting both key and value I would go this way:

arsort($data);
$result = array_slice($data,0,2);
不交电费瞎发啥光 2024-10-14 14:37:14
asort($data);
$first = array_pop($data);
$second = array_pop($data);

请注意,我没有按相反顺序放置并抓取第一个项目,而是按顺序放置并抓取最后一个项目。

asort($data);
$first = array_pop($data);
$second = array_pop($data);

Notice that instead of putting it in reverse order and grabbing the first items, I put it in order and grabbed the last items.

最美不过初阳 2024-10-14 14:37:14

您的代码是正确的,您只需进行一些额外的调用

arsort($data);
$first = each($data);
$second = each($data);

就足够了。

你需要更具体地说明你到底想要得到什么。 each 返回一个由 四个 元素组成的数组 - 这是您想要的吗?

Your code is correct, you just have some extra calls there

arsort($data);
$first = each($data);
$second = each($data);

should be enough.

You need to be more specific though about what exactly you want to get. each returns an array of four elements - is this what you want?

溺孤伤于心 2024-10-14 14:37:14

像这样的东西:

$array = array (
    'wood'  => 2,
    'metal' => 1,
    'blah'  => 4
);

$first  = end($array);
$second = prev($array);

Something like this:

$array = array (
    'wood'  => 2,
    'metal' => 1,
    'blah'  => 4
);

$first  = end($array);
$second = prev($array);
不可一世的女人 2024-10-14 14:37:14

也许不完全是优先级队列的用途和常规数组排序提取函数也完全没问题,但要添加一些新内容:

$queue = new SplPriorityQueue;
$queue->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
$queue->insert('wood', 2);
$queue->insert('metal', 5);
$queue->insert('plastic', 3);

这会自动按数值降序排序。要获取元素, do

var_dump(
    $queue->extract(),
    $queue->extract()
);

会给出 (codepad)

array(2) {  ["data"]=>  string(5) "metal",    ["priority"]=>  int(5) }
array(2) {  ["data"]=>  string(7) "plastic",  ["priority"]=>  int(3) }

并离开 $queue 则仅包含“木材”。

除了 SplPriorityQueue 之外,您还可以使用 SplMaxHeap

阅读更多信息:

Maybe not exactly what a Priority Queue was intended for and regular array sorting and extracting functions are completely fine either, but to add something new:

$queue = new SplPriorityQueue;
$queue->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
$queue->insert('wood', 2);
$queue->insert('metal', 5);
$queue->insert('plastic', 3);

This is automatically sorted in descending order of the numeric value. To get the elements, do

var_dump(
    $queue->extract(),
    $queue->extract()
);

would give (codepad)

array(2) {  ["data"]=>  string(5) "metal",    ["priority"]=>  int(5) }
array(2) {  ["data"]=>  string(7) "plastic",  ["priority"]=>  int(3) }

and leaves $queue containing only 'wood' then.

In addition to the SplPriorityQueue, you could also use an SplMaxHeap.

Read more at:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文