gpt4 book ai didi

php - Array Unique 在 PHP 中的二维数组中

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:33 24 4
gpt4 key购买 nike

您好,我有一个数组,如下所示:

Array ( [0] => Array ( [x] => 01 [y] => 244 ) [1] => Array ( [x] => 02 [y] => 244 ) [2] => Array ( [x] => 03 [y] => 244 ) [3] => Array ( [x] => 04 [y] => 243 ) [4] => Array ( [x] => 05 [y] => 243 ) [5] => Array ( [x] => 05 [y] => 244 ) [6] => Array ( [x] => 06 [y] => 242 ) [7] => Array ( [x] => 06 [y] => 243 ) [8] => Array ( [x] => 07 [y] => 243 ) [9] => Array ( [x] => 08 [y] => 243 ) [10] => Array ( [x] => 09 [y] => 242 ) [11] => Array ( [x] => 10 [y] => 244 ) [12] => Array ( [x] => 12 [y] => 243 ) [13] => Array ( [x] => 13 [y] => 243 ) [14] => Array ( [x] => 13 [y] => 243 ) [15] => Array ( [x] => 15 [y] => 243 ) ) 

x 代表某个变量的天数和 y 值。我想实用地显示一组独特的天数 x(最后一个元素)和值 y。

例如第 6 天,我有两个 y 值,但我只想显示最后一个 ( 243 )。

感谢帮助

最佳答案

执行此操作的一种方法是使用一个简单的循环来生成一个新数组,并在进行时覆盖给定日期的任何现有值。

代码片段

// $data = ... your array from the question
$result = array();
foreach ($data as $value) {
$result[$value['x']] = $value['y'];
}

print_r($result);

输出

Array
(
[01] => 244
[02] => 244
[03] => 244
[04] => 243
[05] => 244
[06] => 243
[07] => 243
[08] => 243
[09] => 242
[10] => 244
[12] => 243
[13] => 243
[15] => 243
)

如果上述方法不适合您,还有许多替代方法(或主题的变体)。

关于php - Array Unique 在 PHP 中的二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2519411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com