3

已解决:请参阅我的答案以了解我如何解决我的问题。

我不确定这是否可能,因为我对数组了解不多,但这里什么都没有。

我想在我的网站上添加广告。

我想我可以找到一种方法来实现多维数组来控制内容。

我想出了这个:

$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(吗?

我没有使用太多数组,所以我不知道如何在 for each 循环中定位它的特定部分。

我希望提出一个 foreach 循环,它会输出如下内容:

<a href="UrlFromArray"><img src="ImageSrcFromArray" alt="TitleFromArray">

<br>

<p>DescriptionFromArray</p>

这是可以实现的吗?

编辑和更新:

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
4

2 回答 2

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>
于 2015-07-29T02:19:17.583 回答
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'],它可以工作:)

于 2015-07-29T16:12:24.023 回答