PHP a neat way to display results without a duplicate header

I have a multidimensional array, and I want to get output that outputs only a unique header to the id and output headers.

$job_rows = array(
                array("id" => 1, "output" => "file01", "output_type" => "FBX"),
                array("id" => 1, "output" => "file01", "output_type" => "JPG"),
                array("id" => 1, "output" => "file03", "output_type" => "JPL"),
                array("id" => 2, "output" => "file05", "output_type" => "FBX"),
                array("id" => 2, "output" => "file06", "output_type" => "JPX"),
                array("id" => 2, "output" => "file06", "output_type" => "JPG"),
                array("id" => 3, "output" => "file010", "output_type" => "FBX"),
                array("id" => 3, "output" => "file010", "output_type" => "JPA")
            );

The result that I want to accomplish.

ID : 1
    OUTPUTS :
    file01  FBX
            JPG
    file03  JPL
ID : 2
    OUTPUTS :
    file05  FBX
    file06  JPX
            JPG
ID : 3
    OUTPUTS :
    file010 FBX
            JPA


.. etc

basically aggregates common identifiers during echo results on a page. Thanks

+3
source share
3 answers

something like that:

$ID = -999;
$FILENAME = "";
foreach ($job_rows as $key => $value) {    
    foreach ($value as $k => $v) {
        print ($ID === $value['id'])?"":"ID : ".$value['id']."<br />";
        print ($FILENAME === "".$value['output'])? "":$value['output']." ";
        print ($k == 'output_type')?$v."<br />":"";
        $ID = $value['id'];
        $FILENAME = "".$value['output'];
    }
}

and it gives the result exactly the way you wanted:

ID : 1
file01 FBX
JPG
file03 JPL
ID : 2
file05 FBX
file06 JPX
JPG
ID : 3
file010 FBX
JPA
0
source

Create a new array / datastructure based on the results with the keys idand output:

$newArray = array();
foreach ($job_rows as $row) {
  $newArray[ $row['id'] ][ $row['output'] ][] = $row['output_type'];
}
var_dump($newArray);
0
source

Simple. , , . , , .

<?php

$currentID = null;
$currentFile = null;
foreach($items as $i) {
    if($currentID != $i['id']) {
         $currentID = $id;
         $currentFile = null;
         echo 'ID : '.$id."\n";
    }
    echo "\tOUTPUTS : \n";
    $file = $i['output'];
    if($currentFile != $file) {
        echo "\t\t$file\t{$i['output_type']}\n";
        $currentFile = $file;
    }
    else {
        echo "\t\t\t\t{$i['output_type']}\n";
    }
}

?>
0

All Articles