As @nevermind points out, the only way is by JOIN in your SQL. Any PHP loop of data as you do is just going to be a bottleneck, in both PHP and MySQL. With an only query with a JOIN you'll get all the results, (use the GROUP BY clause with SUM and COUNT to get the number of the elements) and you'll be able of use ORDER BY. And you only use a query against the DDBB, while with your method you use a lot of queries (1 per row) to get the same result. Believe me, I've seen code in system as which you proposed above, and it's like a straitjacket.
If you wanted more help about using the MySQL clauses, post your code and I'll point you about how to do it.
UPDATE
Lets try a bit with SQLFiddle:
Starting point: Two tables, question and answer, with the following structure:
CREATE TABLE IF NOT EXISTS `answer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question_id` int(11) DEFAULT NULL,
`title` tinytext COLLATE utf8_spanish_ci,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
INSERT INTO `answer` (`id`, `question_id`, `title`) VALUES
(1, 1, 'a1'),
(2, 1, 'a2'),
(3, 1, 'a3'),
(4, 1, 'a4'),
(5, 1, 'a5'),
(6, 2, 'a3'),
(7, 2, 'a2'),
(8, 2, 'a1');
CREATE TABLE IF NOT EXISTS `question` (
`id` int(11) DEFAULT NULL,
`title` tinytext COLLATE utf8_spanish_ci,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
INSERT INTO `question` (`id`, `title`) VALUES
(1, 'question 1'),
(2, 'question 2');
http://sqlfiddle.com/#!2/400994/6
As you see, in the fiddle we get the INNER JOIN with the proper data, as many rows as the JOIN of both table.
With a GROUP BY and a LIMIT, we can get all the values, with no worries:
SELECT
q.id qId
, q.title qTitle
, GROUP_CONCAT( a.id SEPARATOR " ")
FROM question q
INNER JOIN answer a
ON a.question_id = q.id
GROUP BY qId
LIMIT 3
http://sqlfiddle.com/#!2/400994/8
The query above will join the tables, group the values, and then, and only then, limit the number of the resultant rows. As you see, you get all the values, and you can even count the values, sum them, concatenate them... As much as you want:
SELECT
q.id qId
, q.title qTitle
, GROUP_CONCAT( a.id SEPARATOR " ")
, SUM( a.id )
, COUNT( a.id )
FROM question q
INNER JOIN answer a
ON a.question_id = q.id
GROUP BY qId
LIMIT 3
http://sqlfiddle.com/#!2/400994/11
And of course, you may wish order them by number of answers, ASC or DESC:
SELECT
q.id qId
, q.title qTitle
, GROUP_CONCAT( a.id SEPARATOR " ")
, SUM( a.id ) sumIdsValues
, COUNT( a.id ) totalAnswers
FROM question q
INNER JOIN answer a
ON a.question_id = q.id
GROUP BY qId
ORDER BY totalAnswers ASC
LIMIT 3
http://sqlfiddle.com/#!2/400994/12
Other thing is getting the values of the different answers, to create an array to export. In that case, you may compound a JSON from the values with CONCAT:
SELECT
q.id qId
, q.title qTitle
, CONCAT( "[", GROUP_CONCAT( a.id SEPARATOR ", "), "]" ) idArrays
, SUM( a.id )
, COUNT( a.id )
FROM question q
INNER JOIN answer a
ON a.question_id = q.id
GROUP BY qId
LIMIT 3
http://sqlfiddle.com/#!2/400994/14
And you get the field idArrays = [1, 2, 3, 4, 5] as a JSON array you may decode with json_decode() and assign to a var. You may use even more complicated structure, the important thing is knowing what you want to compound, and then write the MySQL clause with CONCAT to compound it, XD