0

I'm trying to re-format / re-order the array results I'm getting from mysql_fetch_array() to give a much simpler array to work with.

At the moment when retrieving results from a DB I use a class that does this:

$return = array();
while ($row = mysql_fetch_array($this->query_result, MYSQL_NUM)) {            
    array_push($return, $row);
}

..this function outputs this array:

Array
(
[0] => Array
    (
        [0] => 2
        [1] => Floral
    )

[1] => Array
    (
        [0] => 5
        [1] => Occasion
    )

)

somehow I want to actually output this:

Array
(
    [2] => Floral
    [5] => Occasion
)

I cant get my head around it! Help!

4 Answers 4

1

Try this

foreach($return as $result){
        $final[$result[0]] = $result[1];
    }
0
1

this will work

foreach ($array as $value){
        $result[$value[0]] = $value[1];
}
0
1

maybe:

        $rows = array();
        $row = array();

        while($fetch = mysql_fetch_assoc($result)){

            while(list($field, $val) = each($fetch)){

                $row[$field] = $val;

            }//end while

            $rows[] = $row;

        }//end while

        return $rows;
1
  • Thanks mate, think I'll go with above though. Commented Feb 16, 2012 at 9:07
0

Basicaly what you need to do is this:

Array[ index ] = value;

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.