0

So I have done some research and fixed most of the issues but I can't find out why this is giving me a Notice: Undefined index: in Line 344 If you guys have any suggestion it would be greatly appreciated :D

Here is my Array:

$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);

And my output: [This is line 344]

<?php
    echo $quote[$random[0]]
?>

1 Answer 1

2

If you only get 1 array key with array_rand(), then you don't get an array back, just the key. As you can see in the manual:

When picking only one entry, array_rand() returns the key for a random entry. Otherwise, an array of keys for the random entries is returned. [...]

So after this you try to access the number as an array which doesn't work. So just remove the index from it, e.g.

echo $quote[$random]
                 //^ See here removed

Since it is a two dimension array you also have to sepcify the second dimesion if you want to use echo to print the value, e.g.

echo $quote[$random]["quote"];
echo $quote[$random]["name"];
Sign up to request clarification or add additional context in comments.

9 Comments

Notice: Array to string conversion in; on line 344 Array
@Shaun'Prawn185Moore If you want to access the quote index use quote otherwise name, e.g. echo $quote[$random]["name"]. Or do: print_r($quote[$random]);
I put that in Rizier and got this again Notice: Undefined index: in 344
@Shaun'Prawn185Moore Are you sure you didn't changed your array anywhere? Code works fine!
@Shaun'Prawn185Moore echo $quote[$random]["name"]; echo $quote[$random]["quote"] ?!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.