I'm trying to complete my assignment and this is the last thing to do now.
I know if I want to print the whole array I can just use foreach
and many different method to print the entire array
foreach($v as $k=>$variable_name) { echo "<p> This is index of $k. value is $variable_name <br/></p>";}
But what if I want to only print each index separately?
I want to do the error message under each form so that is why I want each of them separate.
I tried with $v[0]
and nothing appear.
is there a trick or something am I missing?
I believe you're looking for this: http://php.net/manual/en/function.array-keys.php
Try (from the above page):
If you're using the
foreach
loop then you're probably using an associative array (i.e.$v['inputName']
) So, using$v[0]
wouldn't work because the indexes aren't defined by numbers - they are defined by lettering. You could use aforeach
loop to then associate all the values to the numbered indexes and then us it like that.In that case
$x[0]
would workuse print_r function to print the array :
If you're talking about an associative array, you need to fetch the index directly:
Example:
You can do a print_r($array) to see the array structure nicely formatted:
What you're doing instead is fetch a value by its numerical index, like in
On a further note, you can check beforehand if a key exists using array_key_exists():
You can use php array_keys function to get all keys. If Its Associative Array,
output will be :
Other way is :
array_keys() will print indexes in array.