【发布时间】:2021-04-07 10:17:50
【问题描述】:
我有一个像这样的关联数组[这个数组是两个不同数组的组合]:
我结合数组和通过array_combine()函数
$products = array_combine($one_array_key,$second_array_values);
$products = array(
"arn1" =>"A",
"arn2" =>"A",
"arn3" =>"A",
"arn4" =>"B",
"arn5" =>"B",
"arn6" =>"B"
);
如您所见,数组A 和B 中有两个不同的值。
我想要两个数组由它的键组成。
或者换句话说:比较关联数组的值和匹配值的键将被提取到另一个数组。
所以我的预期结果是:
$A = array("arn1","arn2","arn3");
$B = array("arn4","arn5","arn6");
我的代码:
$products = array_combine($one_array_key,$second_array_values);
$products_distinct_values = array_count_values($product);
$products_distinct_values_keys = array_keys($products_distinct_values);
foreach ($products as $key => $value)
{
// what should I need write there, to get the x numbers of array(s), containing *key* of the same *value of array*
}
【问题讨论】:
-
这能回答你的问题吗? Find all array keys that has same value
-
@El_Vanja 我想获得 x 个数组,作为 Jhon_Doe 的回答。 are unknow not know 的值是动态的
-
建议的副本有一个动态解决方案。
-
我试过了,但我试图在不同的阵列上取得结果
-
@Shani 为什么您认为您需要在全局范围内声明单个变量?我不会这样做,因为你失去了在这个“数组集合”上使用数组函数的能力。可变变量也不是一个好主意。那么在你生成所有这些不可预知的变量名之后,你的代码会发生什么?您的代码如何知道要访问什么?你正在走上一条痛苦的道路。