Changing sequential values of a multidimensional array

Asked

Viewed 1,156 times

0

In the framework I develop (PHP language), I use multidimensional array for HTML reporting.

There is a particular report that I cannot properly sort in the SQL query, having only all the data by which I wish to sort after array formation.

Example of the array:

$dados[] = array(

    $cont++,"<nobr>".$nome."</nobr>",
    "<div align=\"center\">".$this->tipoCliente($r['taxa'])."</div>",
    $this->converte_mes($this->mesSel),
    "<div align=\"right\">".number_format($r['taxa'], 2, ',', '.')."</div>",
    "<div align=\"right\">".number_format($taxa, 2, ',', '.')."</div>",
    "<div align=\"right\">".number_format($dife, 2, ",", ".")."</div>",
    $chk
);

I even managed to reorder the array using the following function:

foreach ($dados as $key => $row) 
{
    $tipo[$key]  = $row[2];
    $val[$key]  = $this->dinheiroInteiro($row[3]);
}

array_multisort($tipo, SORT_ASC, $val, SORT_DESC, $dados);

But I still have some problems with the values, and I would need to reset the counter so that it would be starting from 1 after reordering the array.

Just to explain better then, after using the multisort array_array, I reorder the array by the type of client (A, B, C, D)... and the tiebreaker criterion was 4 column (rate). But after my reordering through Multisort the sequence is lost (because the initial ordering of the dice played through while is undone), because it was done by the name.

Any suggestions?

  • Okay, it’s not exactly clear what you’re up to, but at least I can make a guess. Would you like to renumber array indexes back to "normal" (0,1,2,3...)? If so, just pass the array by array_values()

1 answer

2

I’m still not sure if I understand, but it seems like you want to keep order by "name" as it was generated in the while loop. If so, you could add more of this reference array to the multisort array:

foreach ($dados as $key => $row) 
{
    $ref[$key]  = $row[1]; // ou qualquer outro elemento de referência...
    $tipo[$key] = $row[2];
    $val[$key]  = $this->dinheiroInteiro($row[3]);
}

array_multisort($ref, ...SUA_OPÇÃO..., $tipo, SORT_ASC, $val, SORT_DESC, $dados);

So you would have the array sorted by name, having the second and third options as "tiebreaker".

Browser other questions tagged

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