【问题标题】:PHP Content Rotator Using Multidimensional Arrays使用多维数组的 PHP 内容旋转器
【发布时间】:2015-10-19 19:49:53
【问题描述】:

已解决:请查看我的回答,了解我是如何解决问题的。

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

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

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

我想出了这个:

$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>';

$ads 上执行var_dump 并得到以下结果:

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

【问题讨论】:

    标签: php arrays object multidimensional-array rotator


    【解决方案1】:

    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>
    

    【讨论】:

    • 我按照你的建议想出了一个代码。我现在只是收到错误,这意味着它正在进行中:) 请查看我的编辑?
    • php.net/array_rand "从数组中选择一个或多个随机条目,并返回随机条目的键(或多个键)。"
    • @Jessica 它正在选择它们 :) 但是在我的代码中的某个地方我在尝试 echo 键值时仍然有错误:) 我确信这只是一个简单的修复。
    • 它正在选择一个 KEY。然后,您尝试将其用作对象。
    • 我不知道如何解决这个问题。我尝试过阅读,但我尝试过的没有任何效果。
    【解决方案2】:

    我想通了。我将其发布为答案,以防其他用户遇到类似问题并且可以使用我的问题和答案作为帮助:)

    完整的工作代码:

    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-&gt;$randomAd-&gt;description 定位键,而是用$ads[$randomAd]['description'] 定位它们,它可以工作:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      相关资源
      最近更新 更多