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?

zac1987
  • 2,721
  • 9
  • 45
  • 61
  • `foreach($randomfriends as $c => $friend)` -- you know array_rand only returns keys, right? the `=> $friend` is kind of moot. – Brad Christie Jul 08 '11 at 03:29

3 Answers3

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

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

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • output is still having problem : 'tt', 'hh', 'ii', 'vv', 'uu', 'bb', 'oo', 'dd', 'yy', 'aa', 'ee', 'gg', 'ff', 'qq', 'micellelimmeizheng1152013142', ss, rr, ww – zac1987 Jul 08 '11 at 03:31
  • @zac1987, the issue lies in the rest of your code. See my extended answer that builds upon this with due credit. – Jason McCreary Jul 08 '11 at 03:38
  • 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 Jul 08 '11 at 03:39
  • 1
    @zac1987: if you had 1000 records - you'd better sorted/shuffled them in database and just not fetch the ones you don't need – zerkms Jul 08 '11 at 03:42
  • i cannot shuffle them in database, because ORDER BY RAND() will cause problem when heavy load traffic in database... info at http://stackoverflow.com/questions/6592751/why-dont-use-mysql-order-by-rand – zac1987 Jul 08 '11 at 03:47
  • 1
    @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 – zerkms Jul 08 '11 at 03:49
  • em... "request 1000 temporary tables is created and sorted in database" VS "sort 1000 values in php arrays". Which 1 is more consuming? – zac1987 Jul 08 '11 at 04:03
  • @zac1987: requesting 1000 rows in database and ordering them with `ORDER BY RAND()` is **MUCH CHEAPER**. And I have no idea why you mentioned "1000 temporary tables" – zerkms Jul 08 '11 at 04:05
  • The problem of ORDER BY RAND() is that as sql explain tells you the "Using temporary" and the "Using filesort". For each request a temporary table is created and sorted. Thats a pretty heavy operation. It will probably not matter when your database is not under heavy load but it will cost a lot of performance. – zac1987 Jul 08 '11 at 04:17
  • @zac1987: you're just wrong in case that it is heavier than doing the same in php. Also - 1000 rows easily fit into the memory and being sorted there immediately. – zerkms Jul 08 '11 at 04:19
  • Order BY RAND() takes 2.15 seconds to random select from 660,000 records, if a user select 1000 records, 660 users selects at the same time, thus each of them need to wait 2.15 seconds. If 66000 users, then need to wait for 200 seconds? If each user can have 5000 friends, so increase selected 1000 records to 5000 records per user. And not only need to random select 5000 friends, also need to random select 5000 fans, 5000 Neighbours...Sure the database is under heavy load... – zac1987 Jul 08 '11 at 04:41
  • "Order BY RAND() takes 2.15 seconds to random select from 660,000 records" --- you're shuffling in php just 1000 rows. So compare it to shuffling 1000 rows selected in database. – zerkms Jul 08 '11 at 04:43
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);
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • Who is this *zmayte* character you speak of? :P – alex Jul 08 '11 at 03:41
  • @Jason McCreary. WOW! It works! Thank you very much. I think function array_rand() makes things complicated. – zac1987 Jul 08 '11 at 03:56
  • @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. – Jason McCreary Jul 08 '11 at 11:46
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)
Aleksey Korzun
  • 680
  • 4
  • 7