开发者

PHP neat way of displaying results, without duplicated heading

开发者 https://www.devze.com 2023-03-11 13:49 出处:网络
I have 开发者_运维问答a multidimensional array and I want to have the output that only outputs the unique heading in \"id\" and \"output\" heading.

I have 开发者_运维问答a multidimensional array and I want to have the output that only outputs the unique heading in "id" and "output" heading.

$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")
            );

Output that I want to accomplished.

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


.. etc

basically combining the common IDs during echo'ing the results on page. Thanks


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 output exactly like you wanted:

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


Create new array/datastructure based on the results using the id and output as keys:

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


Simple. Just keep track of the ID and output values, make sure that the IDs are sorted first. You may have to do some extra coding to get the tabbing to be correct.

<?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

精彩评论

暂无评论...
验证码 换一张
取 消