Sorting the contents of an Array

Asked

Viewed 106 times

1

I have the following Array with the relevant data applied:

  1. Card Number
  2. Month (Validity or Maturity)
  3. Year

I get this data from a API but in a "disorganized" way. I would like to treat these values to follow a specific pattern. I tried to use the function sort() of , but without success.

I receive them this way (disorderly/randomly)

Array                                                                                                
(
    [0] => Array 
    (
        [0] => 5522 8600 0000 0000
        [1] => 2020
        [2] => 09
    )

    [1] => Array 
    (
        [0] => 09
        [1] => 5522 8600 0000 0000
        [2] => 2020
    )

    [2] => Array 
    (
        [0] => 5522 8600 0000 0000
        [1] => 20
        [2] => 9
    )                                                                                                    
)

It is noticeable that ...

the values of the keys [0], [1] and [2] are different from others elements. What was meant to be:

  • [0] = Card Number
  • [1] = Month
  • [2] = Year

Therefore, I would like to create/use a function in which it was possible to standardize them in this way:

Array 
(
    [0] => Array 
    (
        [0] => 5522 8600 0000 0000
        [1] => 09
        [2] => 2020
    )

    [1] => Array 
    (
        [0] => 5522 8600 0000 0000
        [1] => 09
        [2] => 2020
    )

    [2] => Array 
    (
        [0] => 5522 8600 0000 0000
        [1] => 9
        [2] => 20
    )
)

regardless of their positions...


Also worth mentioning

Which, according to this scenario, where the year of 2020 is shown with only 2 decimal places, ie, 20 there would be a conflict with the values of Month, but in the case of validity, to obtain the Month without knowing its position (besides receiving them in a disorderly way..)

Array 
(
    [0] => Array 
    (
        ["mes"] => 09
        ["cartao"] => 5522 8600 0000 0000
        ["ano"] => 20
    )
)

However, because there are no "named" keys, simply create conditions, where:

  • value less than or equal to 12 refers to the month

following the number of months we have in 1 year. Already

  • greater than 12 refers to the year

With this I can obtain the respective values, the opposite of the Card number that only use the Algorithm Luhn to validate Credit/Debit card numbers that resolve!

  • 1

    This cannot occur, it is a serious project error that must be corrected in the backend aiming at a standardization of the data. It is serious because if you look for a record of the beginning of the century, for example, the possibility of data being indistinguishable.

  • Like this @Augustovasques ?

  • 1

    has to make the return of this call uniform. Fix this in the forntend is gambiarra.

  • but my purpose is just fix in the back-end, I get these values so desordenada from an external API (another server) and order it to move to the front end ordenada of my project

  • 1

    The backend is an internal process. One of the internal processes of this php code (which is an internal process of your application) is this API in question that is not friendly to the developer because it does not ensure the consistency of the data because it does not present hierarchy and hegemonic structure in the responses to requests, which may imply situations where the information is indecipherable, the cited example of searches for records dating from the beginning of the century until 2012. This API is the one to be corrected or replaced.

  • @Augustovasques understood, the solution must come from the API right? So to get these values in an orderly way I would need to do what I needed to do? (unfortunately this is the only API so far that can provide this data..)

  • 2

    That’s right. You can tell what the API is and provide a link to the function documentation.

Show 2 more comments

1 answer

1


I have made a small process to help you, it is certainly not the best way but it may be your starting point in this ordination.

    <?php

// Dado o array inicial
$arr = [
    [
        0 => '5522 8600 0000 0000',
        1 => '2020',
        2 => '09'
    ],
    [
        0 => '09',
        1 => '5522 8600 0000 0000',
        2 => '2020'
    ],  
    [
        0 => '5522 8600 0000 0000',
        1 => '20',
        2 => '9'
    ]
];

// Vamos normalizar o tamanho dos elementos para eles poderem ser comparáveis com o min e max mais adiante
$arr_normalizado = [];

foreach($arr as $i)
{
    $arr_normalizado[] = [
        0 => str_pad($i[0], 4, "0", STR_PAD_LEFT),
        1 => str_pad($i[1], 4, "0", STR_PAD_LEFT),
        2 => str_pad($i[2], 4, "0", STR_PAD_LEFT)
    ];
}

// Criando o novo array
$arr_new = [];

foreach($arr_normalizado as $i)
{
    // O maior elemento certamente é o número do cartão
    // e o menor elemento ceertamente é o mês (porque já passamos de 2012)
    $ordenado = [
        0 => max($i),
        1 => substr(min($i), -2)        
    ];
    
    // O último elemento que precisamos buscar é o ano
    // como não sabemos em que posição estava no array normalizado
    // é só buscarmos pelo elemento que não é nenhum dos outros dois
    // que já obtemos pelo min e max
    foreach($i as $v)
    {
        if (!in_array($v, $ordenado))
        {
            $ano = (int) $v;
            
            if ($ano < 2000)
            {
                $ano += 2000;
            }
            
            $ordenado[2] = $ano;
        }
    }   
    
    $arr_new[] = $ordenado;
        
}


print_r($arr_new);

Browser other questions tagged

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