如何回显函数返回的关联数组的值

时间:2016-09-16 13:07:12

标签: php arrays random associative

我正在尝试使用关联数组来完成某些事情,正如Stackoverflow上的其他地方所建议的那样,但是我从未使用数组,所以我很难挣扎。我查了一下,但只是比我更困惑!

这里的交易:我想显示一个随机图像作为Worpdress网站的背景,并显示拍摄该图像的摄影师的姓名。因此,我创建了一个功能,其中包括将图像与摄影师相关联的关联数组,以及用于检索照片和摄影师姓名的小脚本。这是我的功能:

function bg_image_info() {

$creditsList = array(
    "1" => "Photographer 1",
    "2" => "Photographer 2",
    "3" => "Photographer 3",
    ...
    ...
    "74" => "Photographer 74"
);

$root = get_stylesheet_directory_uri();
$dir = $root . "/images/bgs/";
$random = mt_rand(1,74); 

$path = $root . "/images/bgs/bg_" . $random . ".jpg";
$credits = $creditsList["" . $random . ""];

return array($path, $credits);

}

效果很好,但有一个问题。我需要在两个不同的地方使用$ path和$ credits这两个值($ path作为" src"属性,$ 34" p"标记),所以我尝试了在进行了一些研究之后,还要写下这两个函数:

function bg_image_path() {
    list($bgPath, $bgCredits) = bg_image_info($path, $credits);
    echo $bgPath;
}

function bg_image_credits() {
    list($bgPath, $bgCredits) = bg_image_info($path, $credits);
    if($bgCredits) {
        echo "Photo " . $bgCredits . "";
    }
}

然后调用我需要重视的每一个。但看起来这两个函数使用不同的$ random值,因为照片和信用不匹配(如果我将mt_rand()替换为固定值以进行测试,则会执行这些操作)。

那么如何回显第一个函数返回的两个值,以便使用相同的$ random值?

我真的非常感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

关联数组使用命名键作为值,我们可以像索引数组一样创建它们。 foreach用于遍历关联数组。

<?php
$colors = array("0"=>"Red","1"=>"Green","2"=>"Blue");
echo "0th element of array is " . $colors["0"];
echo "<br>";
//looping
foreach ($colors as $key=>$value){
    echo "Key=".$key." value=".$value;
    echo "<br>";
}
?> 

<强>输出:

0th element of array is Red
Key=0 value=Red
Key=1 value=Green
Key=2 value=Blue

答案 1 :(得分:0)

当然会发生这种情况,因为你每次都要调用两次函数 时间你想要的路径或信用,从而产生两个不同的 随机值。

我认为不需要这两个最后的函数(bg_image_path()bg_image_credits())。一个简单的解决方法是调用主函数 页面中的某些点(首次使用前)并保留这些变量 在需要时使用。

list($bgPath, $bgCredits) = bg_image_info($path, $credits); 
# [...]
<img src="<?= $bgPath ?>" />
# [...]
<p>Credits: <?= $bgCredits ?></p>

回答你的评论,我完全理解你想保留它 整洁,不要重复自己,但实际上,在这种情况下,你只是 使用功能。重复调用函数的行没有错 在两个或更多的地方。毕竟应该如何使用它:)

无论如何,如果你想通过不同的方式回应你的价值观 功能,你必须在它们之间共享随机数,这样你才能得到 同一件事情。我想到的第一种方法是你生成 自己编号,他们使用两个函数来回应正确的事情 传递给他们这个数字。但是因为你想保持一切正常 电话,我认为你会喜欢这样做,类似于你目前的设置 3个功能。可以做的是完全重写以生成 main函数中的值并将数据保存在其他函数中:

function
bg_image_info()
{
    # note the global to avoid it being local only
    global $bg_random = mt_rand(1,74);
}

function
bg_image_path()
{
    echo get_stylesheet_directory_uri() .
            "/images/bgs/bg_$bg_random.jpg";
}

function
bg_image_credits()
{
    $creditsList = [
        'none',
        "Photographer 1",
        "Photographer 2",
        "Photographer 3",
        # ...
        "Photographer 74"
    ];

    echo $creditsList[$bg_random];
}

<?php bg_image_info(); ?>
<img src="<? bg_image_path() ?>" />
<p>Credits: <? bg_image_credits() ?></p>

或采用面向对象的方法!

class RandomImage
{
    public $path;
    public $credits;

    public function
    __construct()
    {
        $r = mt_rand(1,74);

        $creditsList = [
            'none',
            "Photographer 1",
            "Photographer 2",
            "Photographer 3",
            # ...
            "Photographer 74"
        ];

        $path = get_stylesheet_directory_uri() .
                    "/images/bgs/bg_$r.jpg";

        $credits = $creditsList[$r];
    }
}

<?php $img = new RandomImage; ?>
<img src="<?= $img->path ?>" />
<p>Credits: <?= $img->credits ?></p>
相关问题