-1

I have a very simple Array that contains a link, and the link title. When I print the Values, for some reason the "3rd" value is missing. I have double checked to make sure the 3rd value exists, and it does. I do not understand what is happening? Any suggestions would be great!!!

<?php

$resources = array(
    "$link" => "$link_name",
    "$link1" => "$link_name1",
    "$link2" => "$link_name2",
    "$link3" => "$link_name3",
    "$link4" => "$link_name4"
);

foreach ($resources as $resources_link => $resources_name) {
    echo "<a href=\"$resources_link\">$resources_name</a><br>";
}
?>

The out put looks like: (Missing the 3rd value) Link1 Link2 Link4 Link5

3
  • What values are in all the variables in your array? Commented Apr 15, 2012 at 14:50
  • and why do you need to put your variables in the array in double quotes? Commented Apr 15, 2012 at 14:50
  • Are you sure you never passed that array by reference? That would corrupt the array, and you would need to reset() it before your foreach. Commented Apr 15, 2012 at 14:56

4 Answers 4

2

I tried this code and works perfectly fine:

<?php

$link = "www.google.com";
$link_name = "Google";

$link1 = "www.google1.com";
$link_name1 = "Google1";

$link2 = "www.google2.com";
$link_name2 = "Google2";

$link3 = "www.google.com3";
$link_name3 = "Google3";

$link4 = "www.google.com4";
$link_name4 = "Google4";

$resources = array("$link"=>"$link_name","$link1"=>"$link_name1","$link2"=>"$link_name2","$link3"=>"$link_name3","$link4"=>"$link_name4");
foreach ($resources as $resources_link => $resources_name) {

echo "<a href=\"$resources_link\">$resources_name</a><br>";
}
Sign up to request clarification or add additional context in comments.

Comments

0

I tried your code modifying it a bit and can see all five links:

<?php

$resources = array(
"1"=>"A",
"2"=>"B",
"3"=>"C",
"4"=>"D",
"5"=>"E"
);

foreach ($resources as $resources_link => $resources_name) {

echo "<a href=\"$resources_link\">$resources_name</a><br>";
}

?>

Comments

0

The variable $link is not define, if it is just a string, replace double quotes with single quotes. like:

<?php

$resources = array(
'$link'=>'$link_name','$link1'=>'$link_name1','$link2'=>'$link_name2','$link3'=>'$link_name3','$link4'=>'$link_name4');

foreach ($resources as $resources_link => $resources_name) {

echo "<a href=\'$resources_link\'>$resources_name</a><br>";
}
?>

1 Comment

Thank you for your help everyone. Something was happening with the variables coming from the database and the array was skipping vales/listing in desc order for some reason. I ended up having to use a few if statements, but was able to get the same results.
0

I suspect the value of $link, $link_name, $link1, $link_name1, ... have special HTML characters. Try to change your loop to:

foreach ($resources as $resources_link => $resources_name) {
    echo "<a href=\"" . htmlentities($resources_link) . "\">" . htmlentities($resources_name) . "</a><br>";
}

htmlentities() will sanitize your text, so it will not break any HTML tags.

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.