0

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
  • Simply by $your_array = clock(1,20,20);. Commented Aug 1, 2014 at 9:26
  • It shows NULL from var_dump. Also I have tried this method. Commented Aug 1, 2014 at 9:29
  • @chchk127 No, really, it is working fine. Where is your function defined in your code ? Commented Aug 1, 2014 at 9:31
  • @chchk127 no, it doesn't - codepad.viper-7.com/BMrWAO Commented Aug 1, 2014 at 9:31

4 Answers 4

4
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>';
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this by just calling your function with your parameters like

$time = clock(10,20,30);

Comments

0

try this

$vararr=clock($a,$b,$c);

Comments

0
$time = clock(12, 40, 15);
echo "<pre>";print_r($time);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.