Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将HREF放入php数组_Php_Arrays_Href - Fatal编程技术网

如何将HREF放入php数组

如何将HREF放入php数组,php,arrays,href,Php,Arrays,Href,我正在尝试创建hrefs链接的php数组 但是,当我运行页面时,我收到错误消息: 未定义的偏移量:0 我想知道如何解决这个问题,或者是否有其他方法可以将“HREF”放入数组中 数组代码: <?php $links = array ( "a" => "<a href='variabletypes.php'>link</a>", "b" => "<a href='variabletypes.php'>li

我正在尝试创建hrefs链接的php数组

但是,当我运行页面时,我收到错误消息:

未定义的偏移量:0

我想知道如何解决这个问题,或者是否有其他方法可以将“HREF”放入数组中

数组代码:

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

?>;
试试这个:

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


?>

其关联数组;)

用于迭代该类型的数组

要迭代键和值,您可以使用:

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

echo$key.'-'.$val;//a-您有这个关联数组:

$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>",
);

你有一个带有字母键的关联数组,例如
a,b,c
,但是你试图用数字键访问数组,例如
0,1,2,3
,所以这显然是行不通的。所以我建议你使用foreach循环,你可以在手册中阅读更多关于它的内容:谢谢,它很有用。我让它工作了。不过我只是不想当我点击按钮时,是否可以逐个循环项。例如,此代码一次循环所有项,但希望通过单击按钮循环来循环它们。您可以这样做,但您必须在会话中保存数组,以便在新请求中保存数组,或使用AJAX进行循环。对于OP和其他读者的解释如下:e有帮助,为什么OP必须使用此代码以及此代码的作用解释OP和其他读者会很有帮助,为什么OP必须使用此代码以及此代码的作用只是想知道当我按下按钮时如何显示循环?这是另一个故事。您的按钮会显示在浏览器中,当您单击它时,yo你需要向服务器发送一个AJAX请求。响应将包含你所回复的所有内容。观看一些教程,它们将对你有所帮助。在youtube上搜索AJAX;)
$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];
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
}