已解决:请参阅我的答案,了解我如何解决问题。
我不确定这是否可行,因为我对阵列了解不多但是什么都没有。
我想在我的网站上添加广告。
我想我可以找到一种方法来实现多维数组来控制内容。
我想出了这个:
$ads = array(
"ad1" => array(
title => "Advertisement Title",
url => "http://example.com",
image => "http://example.com/images/example.jpg",
description => "Advertisement Description"),
"ad2" => array(
title => "Advertisement Title",
url => "http://example.com",
image => "http://example.com/images/example.jpg",
description => "Advertisement Description"),
"ad3" => array(
title => "Advertisement Title",
url => "http://example.com",
image => "http://example.com/images/example.jpg",
description => "Advertisement Description")
);
我通过语法检查程序运行此代码并且没有错误,所以我认为我至少在正确的轨道上。
我不明白的是如何编写随机选择其中一个广告的foreach
循环。
我是否需要将"ad1" => array(
更改为ad[1] => array(
?
我还没有使用过多的阵列,所以我不知道如何针对每个循环定位一个特定部分。
我希望得到一个foreach循环,输出类似于:
<a href="UrlFromArray"><img src="ImageSrcFromArray" alt="TitleFromArray">
<br>
<p>DescriptionFromArray</p>
这可以实现吗?
EDIT&amp; UPDATE:
function displayAds728x90() {
$ads = array(
"ad1" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description"),
"ad2" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description"),
"ad3" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description")
);
$randomAd = array_rand($ads);
echo '<a href="'.$randomAd->url.'" target="_blank">';
echo '<img src="'.$randomAd->image.'" alt="'.$randomAd->title.'">';
echo '</a>';
echo '<p>';
echo $randomAd->description;
echo '</p>';
}
displayAds728x90();
根据Dynelight给出的答案,我已经提出了上述代码。
现在我唯一的问题是我收到以下错误:
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 23
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 27
您知道我的完整代码是哪个行号:
<img src="http://www.example.com/images/your_banner_here.png">
<?php
function displayAds728x90() {
$ads = array(
"ad1" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description"),
"ad2" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description"),
"ad3" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description")
);
$randomAd = array_rand($ads);
echo '<a href="'.$randomAd->url.'" target="_blank">';
echo '<img src="'.$randomAd->image.'" alt="'.$randomAd->title.'">';
echo '</a>';
echo '<p>';
echo $randomAd->description;
echo '</p>';
}
displayAds728x90();
?>
有关导致这些错误的原因的任何想法?
更新2:
编辑以下部分并添加了缺少的代码:
$randomAd = array_rand($ads);
echo '<a href="'.$ads->$randomAd->url.'" target="_blank">';
echo '<img src="'.$ads->$randomAd->image.'" alt="'.$ads->$randomAd->title.'">';
echo '</a>';
echo '<p>';
echo $ads->$randomAd->description;
echo '</p>';
在var_dump
上执行$ads
并获得以下内容:
array(3) { ["ad1"]=> array(4) { ["title"]=> string(19) "Advertisement Title" ["url"]=> string(18) "http://example.com" ["image"]=> string(37) "http://example.com/images/example.jpg" ["description"]=> string(25) "Advertisement Description" } ["ad2"]=> array(4) { ["title"]=> string(19) "Advertisement Title" ["url"]=> string(18) "http://example.com" ["image"]=> string(37) "http://example.com/images/example.jpg" ["description"]=> string(25) "Advertisement Description" } ["ad3"]=> array(4) { ["title"]=> string(19) "Advertisement Title" ["url"]=> string(18) "http://example.com" ["image"]=> string(37) "http://example.com/images/example.jpg" ["description"]=> string(25) "Advertisement Description" } }
关闭上面发布的错误的完整页面代码现在:
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 23
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 23
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 27
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 27
答案 0 :(得分:3)
foreach函数用于迭代数组的所有元素。您想获得数组的随机元素。看看这个函数,也许对你有用:
http://php.net/manual/en/function.array-rand.php
你随机获得elemenet并引用它:
<?php $random_element = array_rand ( $ads); ?>
<a href="<?php echo $ads->$random_element->url ?>">
<img src="<?php echo $ads->$random_element->image ?>" alt="<?php echo $ads->$random_element->title ?>"></a>
<p><?php echo $ads->$random_element->description; ?></p>
答案 1 :(得分:0)
我明白了。我发布这个作为答案,以防万一其他用户有类似的问题,可以使用我的问题和答案作为帮助:)
完整的工作代码:
function displayAds728x90() {
$ads = array(
"ad1" => array(
'title' => "Advertisement Title",
'url' => "http://example.com/1",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description"),
"ad2" => array(
'title' => "Advertisement Title",
'url' => "http://example.com/2",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description"),
"ad3" => array(
'title' => "Advertisement Title",
'url' => "http://example.com/3",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description")
);
$randomAd = array_rand($ads);
echo '<a href="'.$ads[$randomAd]['url'].'" target="_blank">';
echo '<img src="'.$ads[$randomAd]['image'].'" alt="'.$ads[$randomAd]['title'].'">';
echo '</a>';
echo '<p>';
echo $ads[$randomAd]['description'];
echo '</p>';
}
displayAds728x90();
我没有使用$ads->$randomAd->description
定位键,而是使用$ads[$randomAd]['description']
定位它们并且它有效:)