I believe your problem is in foreach($links as $key => $value) {
I will organize the code:
$links = array(
"Apache Server" => "www.apache.org",
"Apress" => "www.appress.org",
"PHP" => "www.php.net"
);
$cnt = 0;
foreach($links as $key => $value) {
echo $value .' -$cnt <br/>';
$cnt++;
}
Notice that there is a similarity between:
$key => $value
With this excerpt from array()
:
"Apache Server" => "www.apacue.org"
I think only by =>
could identify the values, in this case, of the $key
and of $value
.
If you thought:
$key = "Apache Server"
$value = "www.apache.org"
That’s correct, now just imagine the same processed for all three existing elements.
But beware!
Is not always $alguma => $coisa
!
You can also use:
<?
foreach($links as $value) {
echo $value .' -$cnt <br/>';
$cnt++;
}
?>
Whenever there is only ONE variable, such as $value
(but could be any name) will return only the result!
That is, it will only contain: "www.apache.org", "www.appress.org", that is, everything after the =>
, even that would be sufficient in this case.
But there’s also a problem!
In case the $cnt
is among '
, this makes PHP does not display what the variable contains.
Imagine this:
<?php
$nome = 'Inkeliz';
echo '$nome';
// Irá exibir: $nome
echo $nome;
// Irá exibir: Inkeliz
echo "$nome";
// Irá exibir: Inkeliz
?>
For the reason of $cnt
be among '
it is not displayed his number, for that just change to:
echo $value .' -'.$cnt.' <br/>';
// Equivalente ao Segundo método mostrado.
Another possibility is:
echo $value ." -$cnt <br/>";
// Equivalente ao terceiro método mostrado.
It would be interesting to search a little to improve the question, this is a loop of a simple array
– Otto
An array is created and then every value of it is printed on the screen (foreach),
$cnt
displays the element number ...– rray
Nothing more than a simple array loop, see more information here http://php.net/manual/en/control-structures.foreach.php
– Guilherme SpinXO
I am voting to close this question as out of scope because the quality is bad and will take an answer where we would have to make long explanations, so that the user understands, since it demonstrates not even know what code is dealing with.
– Wallace Maxters
SO-PT é um site de perguntas e respostas para programadores profissionais e entusiastas[...]
, I think that enthusiasts qualifies well the AP. -6 in 12 minutes is a bit hasty, let’s be flexible. I’m sorry who gave down, [IMHO].– Papa Charlie
I know the quality of the question is not the best, and it’s extremely amateur, but I’m starting with php and I was left with some doubts as to what would be the result of that code. Actually, I was expecting the contents of the array, but I still had some questions and wanted to confirm. I think this forum can also be used for that. Thanks for the answers. Greetings to all.
– JoaoMvaz
What was the specific question? take advantage and read Stack Overflow in Portuguese is a forum?
– rray