Sort 3 arrays according to the first array

Asked

Viewed 34 times

0

I have 3 separate arrays in $Students, $sites, $considerations. I’m trying to sort my data output by $Students array. When I sort the $Students array, the other two arrays that have line-to-line links with the $Students array are incorrect.

As I do to sort the $Tudents array and this reflect on the other two $sites arrays, $consideracoes?

My arrays:

Array $students
(
    [0] => Stew2 Dent
    [1] => Stew1 Dent
    [2] => Stew5 Dent
    [3] => Stew4 Dent
)
******Array $sites
(
    [1] =>      GRH
    [2] =>      STB     GRH
    [3] =>      STB     GRH
    [4] =>      GRH     CON
)
******Array $consideracoes
(
    [0] =>           LT          WF          BA          PE
    [1] =>           BA
    [2] =>           LT          PE
    [3] =>           LT          PE
)

That’s my code. I tried to use asort, but it’s not working.

function printPDF($students, $sites, $considerations)
{ 
    $marginLeft=15;
    $marginRight=15;
    $marginTop=25;
    $pdf = new PDF(); 
    $pdf->AliasNbPages();
    $pdf->AddPage('L'); 
    $pdf->SetMargins($marginLeft, $marginTop,  $marginRight, true);
    $pdf->SetAutoPageBreak(true, $marginTop);
    $fileOutput= "Report.pdf";
    $pdf->SetFont('helvetica','',  10);
    $lineX=12; //space btw answers
    $lineY=5;
    $id=0;

   asort($students, $sites);

for ($i=0; $i <count($sites); $i++) 
    {
        $pdf->SetX($marginLeft+10 );
        $pdf->Cell($lineX,$lineY,++$id,0,0,'L');
        $pdf->Cell($lineX+22,$lineY,$students[$i],0,0,'L');

        $pdf->Cell($lineX+80,$lineY,$sites[$i+1],0,0,'L');

        $pdf->Cell($lineX+100,$lineY,$considerations[$i],0,1,'L');
    }

$pdf->Output('reports/'.$fileOutput, 'F');

}
  • 1

    Personally, I don’t think you have the ideal representation of information, which dramatically complicates what you intend to do. Much better and simpler was to have an array of objects in which each object has student information, or an array of arrays in which each row (or subarray) has student information. So when you order it all goes together and the problem doesn’t even exist.

  • I have an array, but as I have lines in soft I can not use usort.

  • You don’t have 1 array, you have 3 distinct arrays that are actually all related to each other.

  • Yes. line with line. Some hint of how I can do to order them?

No answers

Browser other questions tagged

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