【发布时间】:2014-08-13 05:27:41
【问题描述】:
首先,如果我的语言有问题,请原谅我 - 我仍在学习如何用编程语言说和写。当数组有多个键值对时,如何从 PHP 中的数组中检索整个对象?
<?php
$quotes = array();
$quotes[0] = array(
"quote" => "This is a great quote",
"attribution" => "Benjamin Franklin"
);
$quotes[1] = array(
"quote" => "This here is a really good quote",
"attribution" => "Theodore Roosevelt"
);
function get_random_quote($quote_id, $quote) {
$output = "";
$output = '<h1>' . $quote["quote"] . '.</h1>';
$output .= '<p>' . $quote["attribution"] . '</p>';
return $output;
} ?>
<?php
foreach($quotes as $quote_id => $quote) {
echo get_random_quote($quote_id, $quote);
} ?>
使用 array_rand 和 var_dump 我可以在浏览器中以原始形式查看项目,但我无法真正弄清楚如何让每个元素以 HTML 格式显示。
$quote = $quotes;
$random_quote = array_rand($quote);
var_dump($quote[$random_quote]);
提前感谢您的帮助!
【问题讨论】:
-
你的预期输出是什么?
-
首选输出是随机显示一个报价。