I have created a function that is supposed to return an array of errors to validate a form. I have tried to accomplish this a few ways, but problem I'm having is that I cannot seem to display the error messages.
I've tried
for ($i = 0; $i < count($errors); $i++)
{
echo ($errors[$i]);
}
and
foreach($errors as $err)
{
echo($err);
}
Both of these seem to give me the same result: "ArrayArray".
If I use var_dump()
it does show that the array has values.
I feel like this has a very simple solution, but I h开发者_JS百科ave been searching for quite a while and trying to modify this anyway I can think of with no result. What am I missing?
you have a multi dimensional array. Assuming you have 2d array you would.
foreach($errors as $err)
{
foreach($err as $err2)
echo($err2);
}
or old style
foreach($errors as $i => $err)
{
foreach($err as $errKey => $errVal)
echo($errVal);
}
You can use print_r( $errors );
. Works for arrays, objects etc.
@comment Looking at what print_r shows your table definition is quite weird. To list error msgs you need:
foreach( $errors[2] as $err )
echo $err."<br />";
function displayErrors($errors)
{
if(is_array($errors)){
foreach($errors as $error)
{
displayErrors($error);
}
}
else{
echo($errors);
}
}
An addition to the suggestion by @piotrm - you get nicely readable output if you wrap your print_r()
call in <pre>...</pre>
tags:
<pre>
<?php print_r($errors); ?>
</pre>
It is at least the easiest solution!
精彩评论