-1

I have :

  $years = '2015,2017,2016';
  $short = '21,1,7';
  $long  = '777-21/2015,777-1/2017,777-7/2016';

I need to rearrange them by years, descending. So that I get :

  $years == '2017,2016,2015';
  $short == '1,7,21';
  $long  == '777-1/2017,777-7/2016,777-21/2015';

explode(',',$years), rearranging $years and using the keys as a pattern may be a good solution but I'm not sure how to do it.

Pavel Cechir
  • 111
  • 1
  • 7
  • 1
    hmmmm I smell [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) here, if I'm correct then add the big goal please :) – niceman Feb 21 '17 at 20:25
  • A guess on what @niceman said, why are you not storing the dates in one variable? That way you can sort it really easy... – Nytrix Feb 21 '17 at 20:28
  • strings are user defined inputs values, php part should rearrange them so that no issue occures in future processing – Pavel Cechir Feb 21 '17 at 20:32
  • So you have one text input for day, one for month and one for year ? if that's so then why not have one input for the complete date ? if you don't care about old browsers you can have `` and you're good to go, even if you care one text input is much easier – niceman Feb 21 '17 at 20:37
  • with one text input you can parse the string into a date with javascript or php(pretty sure you'll find a built-in for this) and you can validate the format and tell the user to put it in a specific format you choose – niceman Feb 21 '17 at 20:40
  • It is not about the date, I just need to reorder the data ($short and $long) the same way how years would rearange themself descending. (strings shouls remain strings in all the variables) – Pavel Cechir Feb 21 '17 at 20:44
  • if it's not about the date then we know nothing about X in the XY problem :) – niceman Feb 21 '17 at 20:46
  • ok, rearranging '2015,2017,2016' to '2017,2016,2015' means moving '2017'(second in string) to be first, '2016'(third in string) to become second and '2015'(first in string) to become third. I need to use the same pattern for the remaining two strings $short,$long – Pavel Cechir Feb 21 '17 at 20:51
  • I know what "rearranging" means, what I meant is exactly like the example the answer to the question [here](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) includes – niceman Feb 21 '17 at 20:54

1 Answers1

0

I don't know why Mark Baker deleted his, but this is similar:

$years = explode(',', $years);
$short = explode(',', $short);
$long  = explode(',', $long);

array_multisort($years, SORT_DESC, $short, $long);

$years = implode(',', $years);
$short = implode(',', $short);
$long  = implode(',', $long);
  • explode() the strings into arrays
  • sort $years, sorting the others the same
  • implode() back to strings

NOTE: You should pay attention to the comments and post a larger question to get a better strategy for doing this. Using comma separated values is probably the wrong approach from the start.

Community
  • 1
  • 1
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87