VORAS9319
VORAS9319

Reputation: 13

how to put hrefs into php array

I'm trying to create the php array of hrefs links.

However when Im running the page im getting error message:

Undefined offset: 0

And I would like to know how to fix this, or is there any other way how to put "hrefs" into array.

Array code:

<?php

    $links = array (
        "a" => "<a href='variabletypes.php'>link</a>",
        "b" => "<a href='variabletypes.php'>link</a>",``
        "c" => "<a href='variabletypes.php'>link</a>",;
        "d" => "<a href='variabletypes.php'>link</a>",;
        "e" => "<a href='variabletypes.php'>link</a>",
    );

    for($i=0; $i<sizeof($links);$i++)``
        echo $links[$i];

?>;

Upvotes: 1

Views: 993

Answers (3)

Lajos Arpad
Lajos Arpad

Reputation: 76583

You have this associative array:

$links = array (
    "a" => "<a href='variabletypes.php'>link</a>",
    "b" => "<a href='variabletypes.php'>link</a>",``
    "c" => "<a href='variabletypes.php'>link</a>",;
    "d" => "<a href='variabletypes.php'>link</a>",;
    "e" => "<a href='variabletypes.php'>link</a>",
);

You do not need ; and ` in the array definition. Another problem is:

for($i=0; $i<sizeof($links);$i++)``
    echo $links[$i];

you do not need the ` in the loop. Also, you try to use numerical indexes, however, your indexes are strings. Try to use a foreach loop instead:

foreach ($links as $key => $value) {
    echo $value;
    //note that $key will hold your index. It is optional, but it is good to know it is there
}

Upvotes: 0

user3565074
user3565074

Reputation: 71

its assoc array ;)

Use foreach to iterate thats type of array.

To iterate key and values u can use:

foreach($links as $key => $val) {
    echo $key.' - '.$val; // a - <a href=...
}

or for iterate only values:

foreach($links as $str){
   echo $str; //<a href=...
} 

Upvotes: 0

Ahmed Ziani
Ahmed Ziani

Reputation: 1226

try with this :

<?php
   $links = array ( "a" => "link", "b" => "link", "c" => "link", "d" => "link", "e"=>"link" );
   foreach ($links as $link)
      echo $link;


?>

Upvotes: 1

Related Questions