3

Question : How to display 15 random usernames with single quote on each username in an array?

Code :

$friendsArray = array("zac1987", "peter", "micellelimmeizheng1152013142",.....);
$randomfriends = array_rand($friendsArray, 15); //random select 15 usernames

foreach($randomfriends as $c => $friend){ //wrap each username in a single quote
    $friendsArray[$c] = "'".mysql_real_escape_string($friendsArray[$c])."'";
}

$friendsArray2 = join(', ',$friendsArray);
echo $friendsArray2;

Output :

'hh', 'gg', 'ff', 'dd', 'ss', 'aa', 'oo', 'ii', 'uu', 'yy', 'tt', 'rr', 'ee', 'ww', 'qq', micellelimmeizheng1152013142, vv, bb

The problem can be seen obviously through the output. micellelimmeizheng1152013142, vv, bb are 16th, 17th and 18th entry without single quotes, they supposedly not to be shown, how to delete them?

1
  • foreach($randomfriends as $c => $friend) -- you know array_rand only returns keys, right? the => $friend is kind of moot. Commented Jul 8, 2011 at 3:29

3 Answers 3

6
shuffle($friendsArray);
$rand = array_slice($friendsArray, 0, 15);

And sample: http://ideone.com/hjrcY

Sign up to request clarification or add additional context in comments.

12 Comments

output is still having problem : 'tt', 'hh', 'ii', 'vv', 'uu', 'bb', 'oo', 'dd', 'yy', 'aa', 'ee', 'gg', 'ff', 'qq', 'micellelimmeizheng1152013142', ss, rr, ww
@zac1987, the issue lies in the rest of your code. See my extended answer that builds upon this with due credit.
I already have $randomfriends = array_rand($friendsArray, 15); and it works great. If I have 1000 records and i need to select 15 from them, which codes should be better / faster?
@zac1987: if you had 1000 records - you'd better sorted/shuffled them in database and just not fetch the ones you don't need
@zac1987: is that a joke? You think that ORDER BY RAND() will cause a performance issue, but fetching everything and shuffling in php - will not? o_O
|
4

Your issue is that you are not reseting $friendsArray and merely overwriting certain keys with:

$friendsArray[$c] = "'".mysql_real_escape_string($friendsArray[$c])."'";

Here's your complete, corrected code, borrowing from zerkms:

shuffle($friendsArray);
$rand = array_slice($friendsArray, 0, 15);

foreach($rand as $friend) {
    $sql_output[] = "'".mysql_real_escape_string($friend)."'";
}

$sql_output = join(', ',$sql_output);

3 Comments

Who is this zmayte character you speak of? :P
@Jason McCreary. WOW! It works! Thank you very much. I think function array_rand() makes things complicated.
@zac1987, no problem. I agree in this case the code offered by zermks is a bit more intuitive than array_rand() as it returns the keys. But either way, your original code worked had you stored the sql output in a separate array.
0

If you need to delete everything after the quoted strings, you can use this:

<?php

$sample = "'hh', 'gg', 'ff', 'dd', 'ss', 'aa', 'oo', 'ii', 'uu', 'yy', 'tt', 'rr', 'ee', 'ww', 'qq', micellelimmeizheng1152013142, vv, bb";
echo substr($sample, 0, strripos($sample, "'")+1)

Comments

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.