gpt4 book ai didi

php - 如何使用另一个数组中的值作为键从数组中选择值的子集?

转载 作者:行者123 更新时间:2023-12-01 22:08:46 25 4
gpt4 key购买 nike

这是我的 $keys 数组:

Array
(
[0] => 1
[1] => 3
[2] => 4
)

还有我的$values :

Array
(
[0] => Red
[1] => Orange
[2] => Yellow
[3] => Green
[4] => Blue
)

我想创建一个包含$values中的一些值的新数组使用 $keys 中的值作为键:

Array
(
[1] => Orange
[3] => Green
[4] => Blue
)

显然我可以foreach获取我想要的值,但我想确保我没有忽略过多的 PHP 数组函数中的某些内容。

我用 Google 搜索了这个问题,得到的答案是使用 array_combine ,这不会达到预期的输出。

感谢您的帮助:)

最佳答案

翻转 $keys 数组以作为值键,然后使用 array_intersect_key() :

$result = array_intersect_key($values, array_flip($keys));

$values 返回与翻转的 $keys 具有相同键的值。

关于php - 如何使用另一个数组中的值作为键从数组中选择值的子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29394589/

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