PDA

View Full Version : What Am I doing Wrong?


Dayo
02-01-2008, 03:58 PM
I have a situation as below...

<?php
function keys() {
$b = array('V00', 'V01', 'V02', 'V03');
return $b;
}

$a = keys();
echo $a;
?>


I would have thought it was an easy straightforward situation but I am not getting the array values back to $a. Pasting this into a file and loading to a browsers simply returns "Array".

Please help.

Thanks

kbluhm
02-01-2008, 04:08 PM
If you echo an array, it will tell you that it is an Array, since echoing is essentially casting it as a string.

Try
<?php
function keys() {
$b = array('V00', 'V01', 'V02', 'V03');
return $b;
}

$a = keys();
print_r( $a );
?>
and look at what you get

JohnDubya
02-01-2008, 04:19 PM
Or if you want to be able to echo out each individual entry in the array in a custom manner, you can use foreach().


$a = keys();

foreach ($a as $key => $value) {
echo "The key is $key, and the value is $value.";
}

Dayo
02-01-2008, 04:22 PM
That explains that. Thanks.

I need to construct my example better lol.