0

Could someone please let me know how I can create an array from the MySQL query?

The array should look like this:

<?php
$testLocs = array(
    '1' => array( 'info' => '1. New Random info and new position', 'lat' => 45345345, 'lng' => 44.9634 ),
    '2' => array( 'ino'  => '1. New Random info and new position', 'lat' => 686788787, 'lng' => 188.9634),
    '3' => array( 'info' => '1. New Random info and new position', 'lat' => 88888, 'lng' => 144.9634),
    '4' => array( 'info' => '1. New Random info and new position', 'lat' => 666666, 'lng' => 144.964 )
);
echo json_encode($testLocs);
exit();
?>

Where the text for info and numbers for lat and lng are MySQL $rows.

Any help would be appreciated.

3 Answers 3

1

Read the manual

Example taken from PHP manual

$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
Sign up to request clarification or add additional context in comments.

Comments

1

If I undestand it correctly you need to map mysql array results to some kind of normalized array. Function array_map would be perfect for it:

$array = array_map(function ($row) {
    return array(
        'info'  => $row['info'],
        'lat'   => $row['lat'],
        'lng'   => $row['lng'],
    );
}, $rows);

If you need indexes starting from 1 you should also add this line:

$array = array_combine(range(1, count($array)), $array);

4 Comments

could you please let me know how you would actually use this in a sql query?
I thought you already have results of mysql query. How do you connect to db from php?
I actually do have the results of mysql query. but when I place your code in my while loop in MYSQL query, I only get [] printed on my php page. that is why i asked you to show me an example if possible so I know how array_combine works in relation to while loop etc.
If you mean that your loop is over mysql results than it is not needed here. Function array_map will act like a loop and iterate over each row of results.
1
   $testLocs = array();

   $sql = <<<SQL
            SELECT id, info, position, lat, lng
            FROM tbl_info
SQL;

    $q = mysql_query($sql);    

    while ($r = mysql_fetch_assoc($q)) {
        $testLocs[$r['id']] = $r;
    }

    print_r($testLocs);

Obviously replace the query with the correct one.

PS. This is the mysql way, not the mysqli way. It might be worth looking at both.

7 Comments

aren't you suppose to declare $testLocs somewhere?
Yes by rights you are. this will still work but will throw warnings. I will edit
mysql_* functions are deprecated. please consider using mysqli_* or PDO
This will print out something like this: {"1":{"id":"1","name":"Rooz","lat":"51.5033630","lng":"-0.1276250"}} but I need it to be like this: {"1":{"info":"Rooz","lat":51.503363,"lng":-0.127625}}
Yep, change query accordingly to achieve desired array, mine was a only an example
|

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.