6 replies [Last post]
arbel's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 08/12/2007
Juice: 331
Was this information Helpful?

hello,

I'm working on a module and I need to sort a two dimensional associative array.

the array looks something like this

$arr[] = array('weight' => $weightvar, 'value' => $valvar);

this is added a few times so the array has many of these values.

I'd like the sort the array according to the weight value...

$arr[0] = array('weight' => 5, 'value' => 'test1')
$arr[1] = array('weight' => -5, 'value' => 'test2');
$arr[2] = array('weight' => 0, 'value' => 'test3');
$arr[3] = array('weight' => 7, 'value' => 'test4');

will look like

$arr[0] = array('weight' => -5, 'value' => 'test2');
$arr[1] = array('weight' => 0, 'value' => 'test3');
$arr[2] = array('weight' => 5, 'value' => 'test1')
$arr[3] = array('weight' => 7, 'value' => 'test4');

thanks!

Idan

arbel's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 08/12/2007
Juice: 331
Re: sorting an array

ok..found the sollution:

$sort_arr = array();
    foreach($display_fields AS $uniqid => $row){
        foreach($row AS $key=>$value){
            $sort_arr[$key][$uniqid] = $value;
        }
    }
  
    array_multisort($sort_arr['weight'], constant('SORT_DESC'), $display_fields);

this does what I wanted...

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Re: sorting an array

Hey arbel, this may be disappointing to see how simple it is after you've already figured it out... but you should be able to just do this:

<?php
  usort
($sort_arr, 'uc_weight_sort');
?>

I wrote the uc_weight_sort() function to do just what you're looking for.

arbel's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 08/12/2007
Juice: 331
thanks! how does the

thanks!

how does the uc_weight_sort know according to what field to sort the array?

Idan

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: thanks! how does the

It assumes the array you're trying to sort is an array of arrays each having a key called weight. It only works with arrays that have that key.

arbel's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 08/12/2007
Juice: 331
great!. Thanks

great!.

Thanks

seutje's picture
Offline
Joined: 05/15/2008
Juice: 36
Re: Re: Re: sorting an array

hmm, I can't rly seem to manage to get it to work, keep getting either "Fatal error: Cannot use string offset as an array in /home/l6498lab/public_html/tromark/includes/form.inc on line 680" or "Fatal error: Cannot use object of type stdClass as array in /home/l6498lab/public_html/tromark/sites/default/modules/ubercart/uc_store/uc_store.module on line 2461"

I'm trying to change the order (and captions n stuff) of the 3 price fields, so I made a lil module to use just for this website coz I doubt any other website would need it

function tromark_form_alter($form_id, &$form) {
if ($form_id == "product_node_form") {
  $sign_flag = variable_get('uc_sign_after_amount', FALSE);
  $currency_sign = variable_get('uc_currency_sign', '$');
  $secondary_currency_sign = '€';

  $form['base']['prices']['cost'] = array(
    '#type' => 'textfield',
    '#title' => t('Cost'),
    '#required' => FALSE,
    '#default_value' => $node->cost,
    '#description' => t("Your store's cost."),
    '#weight' => 0,
    '#size' => 20,
    '#maxlength' => 35,
    '#field_prefix' => $sign_flag ? '' : $currency_sign,
    '#field_suffix' => $sign_flag ? $currency_sign : '',
  );
    $form['base']['prices']['sell_price'] = array(
    '#type' => 'textfield',
    '#title' => t('Sell price'),
    '#required' => FALSE,
    '#default_value' => $node->sell_price,
    '#description' => t('Price in dollars.'),
    '#weight' => 1,
    '#size' => 20,
    '#maxlength' => 35,
    '#field_prefix' => $sign_flag ? '' : $currency_sign,
    '#field_suffix' => $sign_flag ? $currency_sign : '',
  );
  $form['base']['prices']['list_price'] = array(
    '#type' => 'textfield',
    '#title' => t('Sell price'),
    '#required' => FALSE,
    '#default_value' => $node->list_price,
    '#description' => t('Price in euros.'),
    '#weight' => 2,
    '#size' => 20,
    '#maxlength' => 35,
    '#field_prefix' => $sign_flag ? '' : $secondary_currency_sign,
    '#field_suffix' => $sign_flag ? $secondary_currency_sign : '',
  );

}
}

Ideally I would want the "cost" input field to disappear and the "list price" field to come after the "sell price" field, but I can't seem to manage to do this :x

here be the end!