dongtong5242 2014-08-01 09:24
浏览 68
已采纳

如何获取数组在函数中创建的数组值?

When I think about the functional of array and I tried to create an array and store in the function acts as the clock but how can I get the array values outside the function?

 function theClock($a,$b,$c){
    $time['Hour'] = $a;
    $time['Minute'] = $b;
    $time['Seconds'] = $c;
    return $time;
 }

  //How can I call the array values in there that $time is stored?

I am a beginner of PHP and I want to improve my concept, I am very grateful if anyone can helps, Cheers!

Sorry, I forgot to mention that I would like to use foreach() to shows the array values as the table form.

  $show1 = '<table border ="1" >';
    foreach($time as $ck => $tk){
        $show1 .= '<tr><td>' . $ck . '</td><td>' . $tk . '</td></tr>';                       
    }
    $show1 .= '</table>';

    $show2 = '<table border ="1">';     
        foreach($time as $tk){
            $show2 .= '<td>' . $tk . '</td>';
            $show2 .= '<td>:</td>';
        }
    $show2 .= '</table>';

        echo $show1;    
        echo $show2;
theClock(11,12,13);
  • 写回答

4条回答 默认 最新

  • duanjiao7440 2014-08-01 09:28
    关注
    function clock($a,$b,$c){
       $time['Hour'] = $a;
       $time['Minute'] = $b;
       $time['Seconds'] = $c;
       return $time;
    }
    
    $time = clock(12, 30, 00);
    $hour = $time['Hour'];
    $minute = $time['Minute'];
    $seconds = $time['Seconds'];
    print $hour; // 12
    print $minute; // 30
    print $seconds; // 00
    

    As per your edit, to use in a foreach loop:

    $html = '<table border ="1" >';
    foreach($time as $unit => $value){
        $html .= "<tr><td>$unit:</td><td>$value</td></tr>";
    }
    $html .= '</table>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里