Hey guys, so I have two arrays
嘿伙计们,所以我有两个阵列
$names = array('jimmy', 'johnny', 'sarah');
$ages= array('16', '18', '12');
I am trying to find the biggest/maximum value in ages, get that elements position and use said position to get the corresponding name. How would I achieve this?
我试图在年龄中找到最大/最大值,获得元素位置并使用所述位置获得相应的名称。我怎么做到这一点?
Is there a better way to achieve the corresponding name, perhaps bundling all in one?
有没有更好的方法来实现相应的名称,也许捆绑在一起?
Thanks
谢谢
3 个解决方案
#1
3
This should get the key for you:
这应该是你的关键:
$names = array('jimmy', 'johnny', 'sarah');
$ages= array('16', '18', '12');
$oldest = array_search(max($ages), $ages);
echo $names[$oldest];
You should note though, that if two persons would have the same age, the first of these two persons would be the one returned.
但是,您应该注意,如果两个人的年龄相同,则这两个人中的第一个将是返回的人。
if you need to find all the oldest you should use array_keys()
instead of array_search()
like this:
如果你需要找到所有最旧的,你应该使用array_keys()而不是array_search(),如下所示:
$names = array('jimmy', 'johnny', 'sarah', 'kristine');
$ages = array('16', '18', '12', '18');
$oldestPersons = array_keys($ages, max($ages));
foreach($oldestPersons as $key) {
echo $names[$key].'<br />';
}
#2
1
Just for fun, here's another solution:
只是为了好玩,这是另一个解决方案:
$names = array('jimmy', 'johnny', 'sarah');
$ages= array('16', '18', '12');
$people = array_combine($ages, $names);
ksort($people);
echo 'Oldest person is: '.end($people);
看到它在行动。
Note: If many people have the same age, the one appearing last in the input arrays gets picked. This is a result of the behavior of array_combine
.
注意:如果许多人具有相同的年龄,则会选择输入数组中最后出现的那个。这是array_combine行为的结果。
#3
0
$oldest_key = 0;
$age_var = 0;
foreach ($ages as $key => $age) {
if ($age > $age_var) {
$age_var = $age;
$oldest_key = $key;
}
}
echo "Oldest person is: {$names[$oldest_key]} ($age_var)";
#1
3
This should get the key for you:
这应该是你的关键:
$names = array('jimmy', 'johnny', 'sarah');
$ages= array('16', '18', '12');
$oldest = array_search(max($ages), $ages);
echo $names[$oldest];
You should note though, that if two persons would have the same age, the first of these two persons would be the one returned.
但是,您应该注意,如果两个人的年龄相同,则这两个人中的第一个将是返回的人。
if you need to find all the oldest you should use array_keys()
instead of array_search()
like this:
如果你需要找到所有最旧的,你应该使用array_keys()而不是array_search(),如下所示:
$names = array('jimmy', 'johnny', 'sarah', 'kristine');
$ages = array('16', '18', '12', '18');
$oldestPersons = array_keys($ages, max($ages));
foreach($oldestPersons as $key) {
echo $names[$key].'<br />';
}
#2
1
Just for fun, here's another solution:
只是为了好玩,这是另一个解决方案:
$names = array('jimmy', 'johnny', 'sarah');
$ages= array('16', '18', '12');
$people = array_combine($ages, $names);
ksort($people);
echo 'Oldest person is: '.end($people);
看到它在行动。
Note: If many people have the same age, the one appearing last in the input arrays gets picked. This is a result of the behavior of array_combine
.
注意:如果许多人具有相同的年龄,则会选择输入数组中最后出现的那个。这是array_combine行为的结果。
#3
0
$oldest_key = 0;
$age_var = 0;
foreach ($ages as $key => $age) {
if ($age > $age_var) {
$age_var = $age;
$oldest_key = $key;
}
}
echo "Oldest person is: {$names[$oldest_key]} ($age_var)";