所以我做了一些研究并修复了大多数问题,但我找不到为什么这会给我一个Notice: Undefined index: in Line 344
如果你们有任何建议,我将不胜感激。D
$quote = array(
1 => array(
"quote" => 'The early bird gets the worm, but the second mouse gets the cheese...',
"name" => 'Stephen Wright'
),
2 => array(
"quote" => 'Your time is limited, so dont waste it living someone elses life.',
"name" => 'Steve Jobs'
),
3 => array(
"quote" => 'When one door closes, another one opens. Or you could jut re-open the closed door. Because thats how doors work.',
"name" => "Anon."
),
4 => array(
"quote" => "The two most important days in your life are the day you are born and the day you find out why.",
"name" => "Mark Twain"
),
5 => array(
"quote" => "Before you criticize someone, you should walk a mile in their shoes, that way when you criticize them, you're a mile away and you have their shoes.",
"name" => "Anon."
),
);
$random=array_rand($quote,1);
和我的输出:[这是第344行]
<?php
echo $quote[$random[0]]
?>
如果您使用array_rand()
只获得1个数组键,那么您不会获得数组,只会获得键。正如您在手册中看到的:
当只选取一个表项时,array_rand()返回随机表项的键。否则,返回随机条目的键数组。[…]
在此之后,你尝试访问数字作为一个数组,这是行不通的。因此只需删除索引,例如
echo $quote[$random]
//^ See here removed
因为它是一个二维数组,如果你想用echo
来打印这个值,你也必须指定第二维数组,例如
echo $quote[$random]["quote"];
echo $quote[$random]["name"];