从foreach中的mysql-select内部排序数组

时间:2013-12-23 17:58:36

标签: php mysql arrays

这是我从mysql-select:

获得的数组
 Array (
  [0] => Array (
    [ADDRESS] => Name
                 Street
                 ZIP CITY
                 COUNTRY
    [PRIMARY] => 0
  )
  [1] => Array (
    [ADDRESS] => Name
                 Street
                 ZIP CITY
                 COUNTRY
    [PRIMARY] => 1
  )
)

密钥PRIMARY由PHP设置

$primary = ($address['address_book_id'] == $_SESSION['customer_default_address_id']) ? 1 : 0;
'PRIMARY'       => $primary

我使用foreach循环逐步完成mysql数组。 如何对数组进行排序,首先打印出PRIMARY = 1的数组元素?

更多代码要求更加谨慎。

$addresses = $db->result("
  SELECT   address_book_id,
           entry_company AS company,
           entry_firstname AS firstname,
           entry_lastname AS lastname,
           entry_street_address AS street_address,
           entry_suburb AS suburb,
           entry_postcode AS postcode,
           entry_city AS city,
           entry_state AS state,
           entry_country_id AS country_id,
           entry_zone_id AS zone_id
  FROM     " . TABLE_ADDRESS_BOOK . "
  WHERE    customers_id = " . (int)$_SESSION['customer_id'] . "
  ORDER BY firstname,
           lastname
");
foreach ($addresses['RESULT'] as $address) {
  $format_id      = inc_getAddressFormatId($address['country_id']);
  $primary        = ($address['address_book_id'] == $_SESSION['customer_default_address_id']) ? 1 : 0;
  $address_data[] = array(
    'ADDRESS'       => inc_getAddressFormat($format_id, $address, true, ' ', '<br>'),
    'PRIMARY'       => $primary,
  );
}

1 个答案:

答案 0 :(得分:0)

如果是来自mysql,我会直接添加:

ORDER BY `PRIMARY` DESC

最后。