1

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];

?>;
3
  • 1
    Well you have an associative array with alphabetic keys e.g. a,b,c, but you try to access the array with numeric keys e.g. 0,1,2,3 , so this is obviously not going to work. So I would recommend you to use a foreach loop, you can read more about it in the manual: php.net/manual/en/control-structures.foreach.php Commented Mar 25, 2015 at 10:49
  • Thanks , it was usefull. I got it working. However just wondering is it possible to loop item one by one when i klick the button. For Example this code loop all item in once but want that they would be looped by clicking button loop<?php $links = array ( "a" => "<a href='variabletypes.php'>Var</a>", "b" => "<a href='Conditionals.php'>Cond</a>", "c" => "<a href='Arrays.php'>Arrays</a>", ); foreach ($links as $loop) echo $loop. "<br>"; ?> <button> loop </button> Commented Mar 25, 2015 at 11:44
  • You can do it, but you would have to save your array in the session to keep it over new requests or do it with AJAX. Commented Mar 25, 2015 at 11:46

3 Answers 3

1

try with this :

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


?>
Sign up to request clarification or add additional context in comments.

1 Comment

An explanation for OP and for further readers would be helpful, why OP has to use this code and what this code does
0

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=...
} 

1 Comment

An explanation for OP and for further readers would be helpful, why OP has to use this code and what this code does
0

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
}

2 Comments

Just wondering how it would be possible to display loop when i press the button ?
That's a different story. Your button is displayed in the browser and when you click it, you need to send an AJAX request to the server. The response will contain everything you have echoed out. Watch some tutorials, they will help you. Search for AJAX on youtube ;)

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.