How to reorder a list using PHP?

Asked

Viewed 61 times

0

Well, good evening guys, I want to make it clear here that I’m not a professional programmer or anything like that, I just pick up a few nozzles that come up and, specifically on this one, I took it for good...

The knowledge I have of php is basic (I think) and this is not allowing me to solve the following problem:

I have this first list here:

05:50 AUDJPY CALL
06:00 EURCHF PUT
08:25 GBPAUD PUT
08:40 USDJPY PUT
10:05 GBPJPY CALL

And she has to "reorder" for that:

AUDJPY 05:50 CALL
EURCHF 06:00 PUT
GBPAUD 08:25 PUT
USDJPY 08:40 PUT
GBPJPY 10:05 CALL

So basically just "reverse" the position of the time with the currency pair

As I don’t know much about php, I ended up discovering several functions present in the language, but none of them helped me (things like explode, implode and wordwrap) and also tried several other possible alternatives, but none I succeeded.

My job is to make a code where:

The client enters the list 1 (in its form) and is returned to the list 2 (reordered)

Any help will be very welcome, I’m already crazy here and I can’t think of anything else

Below is a very useless 'progress' that I did, I used the wordwrap function to break the spacing of some characters in the list, it was even more organized than the original list, but it is not yet what I need...

 <?php


       $lista = "    

05:50 AUDJPY CALL
06:00 EURCHF PUT
08:25 GBPAUD PUT
08:40 USDJPY PUT
10:05 GBPJPY CALL   ";



      $newtext = wordwrap($lista, 15, "<br />");

      echo "$newtext" ; 

      ?>

  • I don’t quite understand the transition from list 1 to list 2. You want to sort list 1 based on time?

  • Lucas Pace, I just want to take the element "currency pair" (that would be the USDJPY and throw it forward, since in the list that the customer will provide, it is in the middle. If you have not understood I can explain again kkk

  • Augusto Vasques, there is in php some function that I can sort of "select from a string to a limit and throw that string to another position then?

  • Ah, Lucas Pace, I think I understand what you mean, no, time has no importance of order there... I just want to change position even

  • 1

    Gee, Augusto, I entered the wrong list 2, I just corrected!

1 answer

3


First you have to break the string into its components so you can regroup them.

Start by clearing the entrance ends by removing the initial and final spaces with trim().
Then separate the text into lines using expode().
For each line obtained again use expode(), this time to separate the line in values. Swap position columns and then using implode() group the values in a new line.
End with implode() forming a new string.

<?php

$lista = "    

05:50 AUDJPY CALL
06:00 EURCHF PUT
08:25 GBPAUD PUT
08:40 USDJPY PUT
10:05 GBPJPY CALL   ";

$lista = trim($lista);            // Tira os espaços das extremidades.
$linhas = explode("\n", $lista);  // Quebra a string em linhas.

//Para cada linha obtida por referência(&)...
foreach($linhas as &$linha){
  $aux = explode(" ", $linha);                       //Quebra as linhas em colunas.
  $linha = implode(" ",[$aux[1],$aux[0],$aux[2]]);   //Troca a posição das colunas e substitui a linha.
}


echo implode("\n",$linhas); // Reagrupa as linhas em uma string.

Upshot:

AUDJPY 05:50 CALL
EURCHF 06:00 PUT
GBPAUD 08:25 PUT
USDJPY 08:40 PUT
GBPJPY 10:05 CALL

See working on Repl.it

Which is not much different from the same example using str_getcsv() instead of exploding.

<?php

$lista = "    

05:50 AUDJPY CALL
06:00 EURCHF PUT
08:25 GBPAUD PUT
08:40 USDJPY PUT
10:05 GBPJPY CALL   ";

$lista = trim($lista);
$linhas = str_getcsv($lista,"\n");

foreach($linhas as &$linha){
  $aux = str_getcsv($linha," ");
  $linha = implode(" ",[$aux[1],$aux[0],$aux[2]]); 
}

echo implode("\n",$linhas);

See working on Repl.it

  • Dude, I don’t know how to thank you for this, you’re an angel sent by Jesus and I’m sure kkkk. Man, I was never gonna figure this out, you can be sure that from now on I’m gonna be able to base my project on a lot of things, my sincere thanks!!!!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.