Split array and sort

3

I have an array as follows:

'Ato001_1981',
'Ato002_1980',
'Ato003_1982',
'Ato003_1983',
'Ato004_1982',
'Ato013_1981',
'Ato013_1982',
'Ato013_1988',
'Ato031_1982',
'Ato032_1979',
'Ato039_1988',
'Ato060_1987',
'Ato065_1988',
'Ato066_1988',
'Ato067_1988',
'Ato076_1987',
'Ato077_1988',
'Ato078_1988',
'Ato095_1987',
'Ato137_1987',
'Ato144_1987'

I would like to break each row of the array to get the name and year and then sort as in the text below:

Ano     Tipo
1988    Ato078
1988    Ato077
1988    Ato067
1988    Ato066
1988    Ato065
1988    Ato039
1988    Ato013
1987    Ato144
1987    Ato137
1987    Ato095
1987    Ato076
1987    Ato060
1983    Ato003
1982    Ato031
1982    Ato013
1982    Ato004
1982    Ato003
1981    Ato013
1981    Ato001
1980    Ato002
1979    Ato032

I tried the code below, but without success:

$new = array();
foreach($texto as $item):
    $valor = explode('_', $item);
    $new['ano'] = $valor[1];
    $new['tipo'] = $valor[0];    
    print_r($new);    
endforeach;
    
asked by anonymous 18.09.2015 / 15:02

3 answers

2

In the most elegant way possible, let's go to the solution:

Your array is as follows:

$dados = array(
    'Ato001_1981',
    'Ato002_1980',
    'Ato003_1982',
    'Ato003_1983',
    'Ato004_1982',
    'Ato013_1981',
    'Ato013_1982',
    'Ato013_1988',
    'Ato031_1982',
    'Ato032_1979',
    'Ato039_1988',
    'Ato060_1987',
    'Ato065_1988',
    'Ato066_1988',
    'Ato067_1988',
    'Ato076_1987',
    'Ato077_1988',
    'Ato078_1988',
    'Ato095_1987',
    'Ato137_1987',
    'Ato144_1987'
);

First let's turn this array . I think the most appropriate function for this is to use array_map , for protecting the scope of the variable and its own functionality for generating arrays based on others.

$array = array_map(function ($value){
    return explode('_', $value);
}, $dados);

Subsequently we will use usort , which compares the values of array , through a callback, in order to sort it.

usort($array, function ($v1, $v2)
{
    $diff = $v2[1] - $v1[1];

    if ($diff) return $diff;

    return strcmp($v2[0], $v1[0]);

});

Finally, we have array - obtained with var_export .

array (
  0 => 
  array (
    0 => 'Ato078',
    1 => '1988',
  ),
  1 => 
  array (
    0 => 'Ato077',
    1 => '1988',
  ),
  2 => 
  array (
    0 => 'Ato067',
    1 => '1988',
  ),
  3 => 
  array (
    0 => 'Ato066',
    1 => '1988',
  ),
  4 => 
  array (
    0 => 'Ato065',
    1 => '1988',
  ),
  5 => 
  array (
    0 => 'Ato039',
    1 => '1988',
  ),
  6 => 
  array (
    0 => 'Ato013',
    1 => '1988',
  ),
  7 => 
  array (
    0 => 'Ato144',
    1 => '1987',
  ),
  8 => 
  array (
    0 => 'Ato137',
    1 => '1987',
  ),
  9 => 
  array (
    0 => 'Ato095',
    1 => '1987',
  ),
  10 => 
  array (
    0 => 'Ato076',
    1 => '1987',
  ),
  11 => 
  array (
    0 => 'Ato060',
    1 => '1987',
  ),
  12 => 
  array (
    0 => 'Ato003',
    1 => '1983',
  ),
  13 => 
  array (
    0 => 'Ato031',
    1 => '1982',
  ),
  14 => 
  array (
    0 => 'Ato013',
    1 => '1982',
  ),
  15 => 
  array (
    0 => 'Ato004',
    1 => '1982',
  ),
  16 => 
  array (
    0 => 'Ato003',
    1 => '1982',
  ),
  17 => 
  array (
    0 => 'Ato013',
    1 => '1981',
  ),
  18 => 
  array (
    0 => 'Ato001',
    1 => '1981',
  ),
  19 => 
  array (
    0 => 'Ato002',
    1 => '1980',
  ),
  20 => 
  array (
    0 => 'Ato032',
    1 => '1979',
  ),
)
    
18.09.2015 / 16:32
2
$texto = array(
    'Ato001_1981',
    'Ato002_1980',
    'Ato003_1982',
    'Ato003_1983',
    'Ato004_1982',
    'Ato013_1981',
    'Ato013_1982',
    'Ato013_1988',
    'Ato031_1982',
    'Ato032_1979',
    'Ato039_1988',
    'Ato060_1987',
    'Ato065_1988',
    'Ato066_1988',
    'Ato067_1988',
    'Ato076_1987',
    'Ato077_1988',
    'Ato078_1988',
    'Ato095_1987',
    'Ato137_1987',
    'Ato144_1987'
);

$new = array();
foreach($texto as $item):
    $valor = explode('_', $item);
    $new = ['ano' => $valor[1], 'tipo' => $valor[0]];

    echo $new['ano'] . '  ' . $new['tipo'] . '<br />';
endforeach;

Here it worked like this.

    
18.09.2015 / 15:33
1

The least bad way to sort the array based on the key is to use usort ()

function cmp($a, $b) {
    return $a["ano"] < $b["ano"];
}


$texto = ['Ato001_1981','Ato002_1980','Ato003_1982','Ato003_1983','Ato004_1982','Ato013_1981'];

$new = array();
$index  = 0;
foreach($texto as $item){
    $valor = explode('_', $item);
    $new[$index]['tipo'] = $valor[0];
    $new[$index]['ano'] = $valor[1];
    $index++;
}

usort($new, "cmp");

echo '<pre>';
print_r($new);

Reference: SOen - Sort php multidimensional array by sub-value

    
18.09.2015 / 15:29