0

After executing a prepared SQL statement in PHP, I've doing a fetchAll(). This results in the following array:

Array
(

    [0] => Array
    (
        [ID] => 2
        [0] => 2
        [latitude] => 38.44560000
        [1] => 38.44560000
        [longitude] => -99.99999999
        [2] => -99.99999999
    )
    [1] => Array
    (
        [ID] => 4
        [0] => 4
        [latitude] => 38.44560000
        [1] => 38.44560000
        [longitude] => -99.99999999
        [2] => -99.99999999
    )
)

I would like my array to look like this:

Array
(

    [0] => array('2','otheritem1details....','38.44560000','-99.99999999'),
    [1] => array('4','otheritem1details....','38.44560000','-99.99999999')
)

Do I have to take the data from the source array and manually compile it into a new array in the correct format? Or is there a way to fetch the data initially in the correct format?

Many thanks.

2
  • possible duplicate of PDO::FETCH_ASSOC What is about PDO::FETCH_ARRAY? Commented Sep 1, 2014 at 19:39
  • It's not quite the same - my desired output is different, see below for more info. Thanks. Commented Sep 1, 2014 at 19:44

1 Answer 1

3

fetchAll() takes a fetch style as an optional first parameter. This defaults to PDO::FETCH_BOTH.

From the manual:

PDO::FETCH_BOTH (default): returns an array indexed by both column name and 0-indexed column number as returned in your result set

What you want is PDO::FETCH_NUM:

PDO::FETCH_NUM: returns an array indexed by column number as returned in your result set, starting at column 0

So your call to fetchAll would look something like $sth->fetchAll(PDO::FETCH_NUM)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Doesn't quite do it though. It comes out like this: Array ( [0] => Array ( [0] => 2 [1] => 38.44560000 [2] => -99.99999999 ) It's a bit neater, but not the same.
Think I was just a little confused by the output of print_r to debug in my browser. One way or another, it's now fixed. So thanks, your answer really helped.

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.