所以我做了一些研究并解决了大部分问题,但我无法找出为什么这会给我一个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]]
?>
答案 0 :(得分:2)
如果您只使用array_rand()
获得1个数组密钥,那么您不会返回数组,只有密钥。正如您在manual:
选择仅一个条目时,array_rand()会返回随机条目的键。否则,返回随机条目的键数组。 [...]
所以在此之后你尝试将数字作为一个不起作用的数组来访问。所以只需从中删除索引,例如
echo $quote[$random]
//^ See here removed
由于它是一个二维数组,如果你想用echo
来打印这个值,你还必须分离第二个维数,例如
echo $quote[$random]["quote"];
echo $quote[$random]["name"];