0

I have a MySQL query done using PHP, result is presented as array.

$stmt = $mysqli->prepare("SELECT word FROM words ORDER BY RAND() LIMIT 5");
    $stmt->bind_result($words);

$stmt->execute();
$result = array();

while ($stmt->fetch()) {
    $w = new StdClass();
    $w->word = $words;
    array_push($result, $w);
}
$stmt->close();

Then, i'm passing the array to javascript using JSON:

'words' : <?php echo json_encode($result); ?>,

But the output is:

[{"word":"Watermelon"},{"word":"Orange"},{"word":"Melon"},{"word":"Cucumber"},{"word":"Apple"}]

Is there any way to "strip" the "word" and make it look like that?

["Watermelon", "Orange", "Melon", "Cucumber", "Apple"]

Thank you in advance.

1 Answer 1

2

If you don't need array of objects ($ws) further in your code, you can simplify your code to:

$stmt = $mysqli->prepare("SELECT word FROM words ORDER BY RAND() LIMIT 5");
$stmt->bind_result($words);

$stmt->execute();
$result = array();

while ($stmt->fetch()) {
    array_push($result, $words);
}
$stmt->close();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Apparently i overcomplicated things :)
If answer helps - feel free to accept it, as answers for your previous questions. It helps both you and answerer)

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.