0

I tried to return MYSQLi data into an array but it doesn't get into the form I want.

$sql = <<<SQL
SELECT *
FROM pricee 
ORDER BY idd ASC
SQL;


if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}

while($row = $result->fetch_assoc()){
$price=$row['price'];


$tt=(array)$price;
$i=(array)$row['idd'];
$p=array_combine($i,$tt);
print_r($p);
}

This gives me $p in the following form

Array ( [1] => "0.99" ) Array ( [2] => "0.47" ) Array ( [3] => "0.49" ) 

But I need it in the following:

Array ( [1] => 0.99 [2] => 0.47 [3] => 0.49) 

How can I convert it in that form?

2
  • 1
    So you want to convert strings to floating point numbers in PHP and combine them into a single array? Commented Jun 26, 2014 at 21:49
  • basically I need the values returned from the database in one (numbered) array (see above please) Commented Jun 26, 2014 at 23:16

1 Answer 1

1

Easiest:

while($row = $result->fetch_assoc()){
    $p[$row['idd']] = $row['price'];
}

I don't know if it was intentional, but if you are also showing a string to float conversion, then:

    $p[$row['idd']] = (float)$row['price'];  // or use floatval()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but it doesn't seem to work. It returns the values in the following form: Array ( [0] => 0.49 ) Array ( [0] => 0.99 ) but it should be only one Array
No, the code I posted creates one array. Remove any code you have and check.

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.