用foreach替换while循环

I have a php code like this:

$categories_query = tep_db_query("select categories_id, categories_name from categories order by categories_name");
while ($categories = mysql_fetch_array($categories_query)) {
$categories_array[] = array('id' => $categories['categories_id'], 'text' => $categories['categories_name']);
}

question is how can I replace the while loop with for example foreach so I can first fetch mysql array, then I want to edit some values and then pass it on to a loop? I tried different versions of loops but they don't give me the same result as the while loop does.

what you have tried looks good. there is no "fetch all" function in the old and deprecated mysql library. you should switch to mysqli or PDO instead.
in PDO you can just grab all the result-data with $statement->fetchAll() for example.

if you still want to solve your problem with the old mysql library, then:

$categories_query = tep_db_query("select categories_id, categories_name from categories order by categories_name");
while ($categories = mysql_fetch_array($categories_query)) 
{
    $categories_array[] = array('id' => $categories['categories_id'], 'text' =>     $categories['categories_name']);
}

// do something with your $categories_array here

foreach($categories_array AS $array => $row)
{
    // you can output / access each row here e.g. with: $row['id']
    // or you can do a second foreach-loop for the columns:
    foreach($row AS $col => $data)
    {
        echo $data;
    }
}

PHP doesn't have a mysql_fetch function that delivers all rows at once which could be used in a foreach look.

Try to get all rows in one loop and build an array with all rows. Then, iterate over this array and perform operations as you described.

From the looks of it the purpose of your loop is just to rename some table columns.

Just do:

<?php
  $categories_array = mysqli_query( mysqli_fetch_assoc($con,"SELECT categories_id AS id, categories_name AS text FROM categories ORDER BY categories_name") );
?>