0

Well, I know there was already a similar question before, but I think, this one is a bit different and/or specific. The following Code always returns the correct value-positions within my "$targetArray" (1,2,3,4,5,...), but not its value (*,::,:::,-,...).

function BlaBla($text, $chars = 40) {
$targetArray = array(" * ", " :: ", " ::: ", " - ", " # ", " ", " ", " ", " / ", " \\ ", " ++ ", " ~ ");
$rand = array_rand($targetArray);
 $text = $text." ";
 $text = substr($text,0,$chars);
 $text = substr($text,0,strrpos($text,' '));
 $text = $text." ".$rand;
 return $text;
 }

So: Where is the hook? Any hints for this?

2
  • 4
    When picking only one entry, array_rand() returns the __key__ for a random entry. Commented Aug 4, 2016 at 11:46
  • Well, I didn´t catch your sentence completely, because I´ve already used "array_rand()" in the same way before (I thought so) - anyway @apokryfos answer was perfect for me. Thanks to both of you! Commented Aug 4, 2016 at 12:05

1 Answer 1

1

From the array_rand manual page

Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.

In your case you need:

$randInd = array_rand($targetArray);
$rand = $targetArray[$randInd];
Sign up to request clarification or add additional context in comments.

1 Comment

Wow - it works, great! Two lightning fast answers within three minutes - perhaps this question was too easy... ;-D .....Thanks a lot!

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.