Special array order

27 views
Skip to first unread message

Cesar Ramos

unread,
Jun 29, 2016, 10:04:20 PM6/29/16
to professional-php
Hi, i am trying to do a sorting in php or mysql of the following way:

i have in my db the following

 $array = array(
    array('id' => 1, 'size' => 'G'),
    array('id' => 2, 'size' => 'G'),
    array('id' => 3, 'size' => 'M'),
    array('id' => 4, 'size' => 'M'),
    array('id' => 5, 'size' => 'P'),
    array('id' => 6, 'size' => 'P'),
    array('id' => 7, 'size' => 'P'),
    array('id' => 8, 'size' => 'p'),
  array('id' => 9, 'size' => 'M'),
    array('id' => 10, 'size' => 'M')    
  );

if i order by size DESC i will get something like

 $array = array(
    array('id' => 1, 'size' => 'G'),
    array('id' => 2, 'size' => 'G'),
    array('id' => 3, 'size' => 'M'),
    array('id' => 4, 'size' => 'M'),
  array('id' => 9, 'size' => 'M'),
    array('id' => 10, 'size' => 'M'),
    array('id' => 5, 'size' => 'P'),
    array('id' => 6, 'size' => 'P'),
    array('id' => 7, 'size' => 'P'),
    array('id' => 8, 'size' => 'P')    
  );

but what i need is something like

 $array = array(
    array('id' => 1, 'size' => 'G'),
    array('id' => 3, 'size' => 'M'),
    array('id' => 5, 'size' => 'P'),
    array('id' => 2, 'size' => 'G'),
    array('id' => 4, 'size' => 'M'),
    array('id' => 6, 'size' => 'P'),
  array('id' => 9, 'size' => 'M'),
    array('id' => 7, 'size' => 'P'),
    array('id' => 10, 'size' => 'M'),
    array('id' => 8, 'size' => 'P')    
  );

any ideas?

regards

L. J. Montani

unread,
Jul 19, 2016, 2:32:03 PM7/19/16
to professi...@googlegroups.com
Did you try adding a 'sort' parameter?

$array = array(
    array('id' => 1, 'size' => 'G', 'sort'=1),
    array('id' => 3, 'size' => 'M, 'sort'=2'),
    array('id' => 5, 'size' => 'P', 'sort'=3),
    etc etc
  );

--
This group is managed by the web application development team at www.360psg.com
---
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to professional-p...@googlegroups.com.
Visit this group at https://groups.google.com/group/professional-php.
To view this discussion on the web visit https://groups.google.com/d/msgid/professional-php/CAKsx7mZEad2TO-jx_OeMQYLR01ZRkD%2BHCfatTNg_UUYPwuUDPQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages