问题
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!
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);
}
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 . "";
}
}
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).
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!
回答1:
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.
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 :)
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>
回答2:
Associative arrays uses named keys for values and we can create them in similar way like indexed arrays. foreach is used to loop through an associative array.
<?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>";
}
?>
Output:
0th element of array is Red
Key=0 value=Red
Key=1 value=Green
Key=2 value=Blue
来源:https://stackoverflow.com/questions/39532304/how-to-echo-values-of-an-associative-array-returned-by-function