PHP foreach问题

来源:百度知道 编辑:UC知道 时间:2024/06/05 06:25:37
小白问题 因问PHP中有两个foreach所以不知道该怎么做
最后输出$uStr的结果是
42
24
2
4
应该就是在注释的地方加代码就行了 最好不要改动现在的代码只添加
谢谢了!!!

<html>
<head> <title></title></head>
<?php
function unique($s_arr) {
$us_arr = array();
foreach ($s_arr as $s) {
foreach ($us_arr as $us) {
//complete the missing lines (2 lines needed)
}
return $us_arr;
}
?>

<body>
<?php
$str = array(42, 24, 2, 4, 42, 24, 2, 4, 24, 42, 42, 24);
$uStr = unique($str);
//add code here so that the content of $uStr is printed out (2 lines needed)

?>
</body>
</html>

//complete the missing lines (2 lines needed)
这里修改为
if(!in_array($s, $us_arr))
$us_arr[] = $s;

//add code here so that the content of $uStr is printed out (2 lines needed)
这里修改为
foreach($uStr as $v)
echo $v."\r\n";

不用这么麻烦的.直接用array_unique($str),就能完成你要的功能.但如果你是考试,呵呵,答案就不告诉你了.