如何回显函数返回的关联数组的值 [英] How to echo values of an associative array returned by function

查看:108
本文介绍了如何回显函数返回的关联数组的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用关联数组来完成某些任务,如Stackoverflow上其他地方所建议的那样,但是我从未使用过数组,所以我很挣扎.我已经看过了,但只剩下比以前更困惑了!

I'm trying to accomplish something using an associative array as suggested somewhere else here on Stackoverflow, but I've never used arrays so I'm struggling. I've looked it up but only to be left more confused than I was!

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

Here's the deal: I want to display a random image as the background of a Worpdress site, AND show the name of the photographer who took the image as well. So I've created a function that includes an associative array that associates the image with the photographer, and a little script to retrieve both the photo and the photographer's name. This is my function:

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"属性,$ credits在"p"标签中),因此在进行一些研究后我尝试写另外两个功能:

It works pretty well, but there's one catch. I need to use the two values $path and $credits in two different places ($path as a "src" attribute, $credits in a "p" tag), so what I tried to do after some research was write these two more functions:

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()替换为固定值,它们会匹配).

and then call each one where I need to value to be. But it looks like the two functions use a different $random value because the photo and the credits don't match (they do if I replace mt_rand() with a fixed value for test purposes).

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

So how do I echo the two values returned by the first function so that the same $random value is used?

真的很感谢您的帮助,谢谢!

I would really really appreciate any help, thank you!

推荐答案

当然会发生,因为您两次调用该函数,每次 需要路径或功劳的时间,从而产生两个不同的 随机值.

Of course that happens because you are calling the function twice, each time you want either the path or credits, thus generating two different random values.

我看不到需要这最后两个功能(bg_image_path()bg_image_credits()).一个简单的解决方法是在以下位置调用主函数 在页面上的某些位置(首次使用之前),只需保留这些变量 在需要时使用.

I don't see the need for these two last functions (bg_image_path() and bg_image_credits()). A simple fix is to call your main function at some point in your page (before first use) and just keep those variables to use when needed.

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


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


Answering your comment, I completely understand that you want to keep it tidy and don't repeat yourself, but really, in this case you're just using a function. Nothing wrong repeating a line that calls a function in two or more places. That's how it is supposed to be used after all :)

无论如何,如果您想通过不同的方式来回馈您的价值观 函数,您必须共享它们之间的随机数,以便获得 同样的事情.我想到的第一种方法是您生成 给自己编号,他们使用两个函数来回显正确的内容 向他们传递此号码.但是既然你想保持所有功能 通话,我想您会更喜欢与您当前的设置类似 3功能.可以做的是完全重写以仅生成 主函数中的值,并将数据保留在其他函数中:

Anyways, if you want to echo your values by means of different functions, you have to share the random number between them so you get the same thing. First way that comes to my mind is you generate the number yourself and them use two functions to echo the correct thing by passing them this number. But since you want to keep it all in function calls, I think you will prefer to do it similar to your current setup of 3 functions. What could be done is a complete rewrite to just generate the value in the main function and keep the data in the other ones:

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>

或者采用面向对象的方法!

Or go with an object oriented approach!

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>

这篇关于如何回显函数返回的关联数组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆