0

This is the array I get from the mysql-select:

 Array (
  [0] => Array (
    [ADDRESS] => Name
                 Street
                 ZIP CITY
                 COUNTRY
    [PRIMARY] => 0
  )
  [1] => Array (
    [ADDRESS] => Name
                 Street
                 ZIP CITY
                 COUNTRY
    [PRIMARY] => 1
  )
)

The key PRIMARY is set by PHP with

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

The mysql-array I step through with a foreach loop. How is it possible to sort the array that the array element with PRIMARY = 1 will be printed out at first?

More code to ask more precisly.

$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,
  );
}
Ronny Linsener
  • 253
  • 3
  • 16
  • possible duplicate of [Sort multidimensional array by value (2)](http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2) – jeroen Dec 23 '13 at 18:21

1 Answers1

0

If it is coming from mysql, I would do it there directly by adding:

ORDER BY `PRIMARY` DESC

at the end.

jeroen
  • 91,079
  • 21
  • 114
  • 132