将数组值赋给变量PHP
对PHP来说有点陌生我一直在玩弄它,但我不确定如何将数组的字符串值分配给变量并打印它。目前,它只显示阵列号,而不显示其数据 感谢您的帮助/解释 我目前的代码是:将数组值赋给变量PHP,php,Php,对PHP来说有点陌生我一直在玩弄它,但我不确定如何将数组的字符串值分配给变量并打印它。目前,它只显示阵列号,而不显示其数据 感谢您的帮助/解释 我目前的代码是: <?php $family_friends = array(); array_push($family_friends, "James "); array_push($family_friends, "Patrick"); array_push($family_friends, "Kevin"); array_push($fa
<?php
$family_friends = array();
array_push($family_friends, "James ");
array_push($family_friends, "Patrick");
array_push($family_friends, "Kevin");
array_push($family_friends, "Miles");
array_push($family_friends, "Reuben");
sort($family_friends);
// Randomly select a winner!
$winner = array_rand($family_friends, 1);
// Print the winner's name in ALL CAPS
strtoupper($winner);
echo " ". "Congratulations"." ".($winner) . "!";
?>
数组返回索引,而不是元素。因此,必须在随机索引处选择数组的元素。像这样
strtoupper($family_friends[$winner]);
如果$winner等于零,$family_friends[$winner]等于James。返回一个随机索引,而不是随机元素。您需要将其返回值索引到数组中。您还需要将strtoupper的结果分配给一个变量。因此:
strtoupper($winner);
变成:
$winner = strtoupper($family_friends[$winner]);
这个问题似乎离题了,因为读了这本书就会给出答案。@vascowhite-我不认为这是一个离题的原因:-@SeanBright这就是“其他”的美。你可以自己编:谢谢你的帮助,相信我理解得很好,现在还没有意识到它返回了索引值,非常感谢。