将$ key存储为数组奇怪的结果[重复]

时间:2022-10-07 21:28:08

This question already has an answer here:

这个问题在这里已有答案:

$highest = max($data);
$check = array();
$resultype = array();
foreach($data as $key => $value){
if($value === $highest){
echo $key;

//output (t1,t3);

$check = $key;
}
}

echo $check; 

//ouput(t3);

Why is it working when I store the $key into a array ?
When I echo in $key in foreach I get what I want (t1,t3) but when I store into and array and output it out side foreach, it only give me (t3).
How do I fix this and store $key into a array with the result I wanted?

当我将$ key存储到数组中时,为什么它工作?当我在foreach中回显$ key时,我得到我想要的东西(t1,t3),但是当我存储到数组并将其输出到foreach时,它只给我(t3)。我如何修复此问题并将$ key存储到具有我想要的结果的数组中?

1 个解决方案

#1


You're not storing $key in the array, you're assigning it to the variable $check.

你没有在数组中存储$ key,你将它分配给变量$ check。

Try using $check[] = $key;.

尝试使用$ check [] = $ key;。

#1


You're not storing $key in the array, you're assigning it to the variable $check.

你没有在数组中存储$ key,你将它分配给变量$ check。

Try using $check[] = $key;.

尝试使用$ check [] = $ key;。