试图找出如何用数组写这个

时间:2009-08-17 06:13:01

标签: php

这是我遇到问题的代码

$key = array(
"0" => "sss",
"1" => "wst",
"2" => "sfv",
"3" => "lac",
"4" => "sgv",
"5" => "lgb",
"6" => "ant"
);

$urls = array(
"0" => "http://www.sarmenhb.com/index.php?key=",
"1" => "http://www.navidoor.com/index.php?key=",
"2" => "http://www.worldexchange.com/index.php?key=",
"3" => "http://www.iaddesign.com/index.php?key=",
"4" => "http://www.iadesignandstudio.com/index.php?key=",
"5" => "http://www.redlineautoleasing.com/index.php?key="

);

for($a=0;$a <= count($urls);$a++) {
foreach($key as $keys) {
print $urls[$a].$keys[$a]."<br/>";

}
}

print "<br/><br/>";

我想让输出看起来像这样:

http://www.sarmenhb.com/index.php?key=sss
http://www.navidoor.com/index.php?key=sss
http://www.worldexchange.com/index.php?key=sss
http://www.iaddesign.com/index.php?key=sss
http://www.iadesignandstudio.com/index.php?key=sss
http://www.redlineautoleasing.com/index.php?key=sss

http://www.sarmenhb.com/index.php?key=wst
http://www.navidoor.com/index.php?key=wst
http://www.worldexchange.com/index.php?key=wst
http://www.iaddesign.com/index.php?key=wst
http://www.iadesignandstudio.com/index.php?key=wst
http://www.redlineautoleasing.com/index.php?key=wst

etc including all key values included as a value the the param key

我删除了原始网址,以防止网址黑客攻击,但我怎样才能打印出这样的输出?

我得到的输出是key = s或key = w整个键值不显示。以及错误注意:未初始化的字符串偏移:第32行的D:\ wamp \ www \ MVC \ t.php中的3

请帮助

非常感谢!

4 个答案:

答案 0 :(得分:4)

foreach($key as $string)
{
    foreach($urls as $address)
    {
        echo $address . $string . "<br/>";
    }
}

答案 1 :(得分:3)

我只会做两个foreach声明:

foreach($urls as $url) {
  foreach($keys as $key) {
    print $url.$key."\n";
  }
}

为了简单起见,我还建议您复数数组的名称,检查输出here

答案 2 :(得分:0)

那是因为你正在打印$keys[$a]。 $ keys是数组中的一个字符串,$ a只能得到一个字符。您可以选择字符串中的字符,方法与从数组中选择变量的方式相同。

如果删除方括号,它应该有效。

print $urls[$a].$keys."<br/>";

答案 3 :(得分:0)

删除foreach循环并将$ keys [$ a]更改为$ key [$ a]。