繁体   English   中英

使用自定义循环并排打印 PHP 中的两个多维 Arrays

[英]Printing Two Multidimensional Arrays in PHP with Custom Loop Side by Side

我在 PHP 中有两个多维 arrays 有时会变成 4 级深。

这些 arrays 由 JSON API 构建而成,需要在表格中并排打印,以便每天进行比较。

到目前为止,我已经使用自定义递归循环来打印它们,因为 array_walk_recursive 不会在具有自定义循环能力的表标签中打印它们。

这是我的循环:

 function pretty_dump($arr, $d=1){
                if ($d==1) echo "<pre>";    // HTML Only
                if (is_array($arr)){
                    foreach($arr as $k=>$v){
                        for ($i=0;$i<$d;$i++){
                            echo "_";
                        }
                        if (is_array($v)){
                            echo '<span class="red">'. $k.PHP_EOL . "</span>";
                            Pretty_Dump($v, $d+1);
                        } else {
                            echo '<span class="label"><strong>' . $k . "</strong>" ."</span>"."\t".'<span 
                            class="value">'.$v.PHP_EOL. "</span>";
                        }
                        
                    }
                }
                if ($d==1) echo "</pre>";   // HTML Only

然后我在表内调用这样的数组:

                 <tr>
                    <td>
                        <?
                            pretty_dump ($result);
                        ?>
                    </td>
                    <td>
                        <?
                            pretty_dump ($result2);
                        ?>
                    </td>
                    <td>
                        <?
                            pretty_dump ($result3);
                        ?>
                    </td>
                    
                </tr>

我的问题是我需要将所有三个 arrays($result、$result2、$result3)与 $result(第一个数组)的键进行比较。 我需要以某种方式设置表,以便如果 $result 中的某个键没有值,则 $result 旁边的列中应该有一个 NA 或空白。

例如,如果 $result2 没有对应的 $result 键的值,它应该在 $result2 的 td 标记中打印一个 NA。

所以经过大量的试验和错误,我想通了:

我将 arrays 展平,将我想要的部分一一挑出来,请看下面的工作代码:

<?
                $GLOBALS['seconddata'] = $result2; 
                $GLOBALS['thirddata'] = $result3;
                $GLOBALS['fourthdata'] = $finalresults5;



            function pretty_dump($arr, $d=1, $parentKey=null) {
               
                    function flatten($array, $prefix = '') {
                        $return = [];
                        foreach ($array as $key => $value) {
                            if (is_array($value)) {
                                
                                $return = array_merge($return, flatten($value, $prefix . $key . '_'));
                            } else {
                                $tableRow = "<tr>";

                                $brackets = $prefix . $key;
                                

                                $tableRow .= "<td id='Firstdata'> {$brackets} </td>";
                                
                                $tableRow .= "<td> {$value} </td>";
                                $splitResult = explode("_", $brackets);
                                
                                $output = $GLOBALS['seconddata'];
                                $output2 = $GLOBALS['thirddata'];
                                $output3 = $GLOBALS['fourthdata'];

                                
                                  
                                foreach( $splitResult as $key2 => $value2){
                                     

                                    if(isset($output[$value2])){
                                        $output = $output[$value2];
                                    }
                                    else{
                                        $output = '<span class="red">Non-Existant</span>';
                                    }

                                    if(isset($output2[$value2])){
                                        $output2 = $output2[$value2];
                                    }
                                    else{
                                        $output2 = '<span class="red">Non-Existant</span>';
                                    }
                                    

                                    

                                }
                                
                                if(isset($output3[$brackets])){
                                    $output3 = $output3[$brackets];
                                }
                                else{
                                    $output3 = '<span class="red">Non-Existant</span>';
                                }
                                $difVal = "";
                                if($output != $value){
                                    $difVal = "difference-cell"; 
                                }
                                $tableRow .= '<td id="seconddata" class="' . $difVal . '" >' . $output . "</td>";
                                

                                
                                $tableRow .= '<td id="thirddata">' . $output2 . "</td>";
                               
                                $tableRow .= '<td id="fourthdata">' . $output3 . "</td>";

                                $tableRow .= "</tr>";
                                echo $tableRow;
                            }
                        }
                        return $return;
                    }

                    flatten($arr);
            }
            
                        
        ?>
        <table class="table">
            <thead>
                <tr>
                    <th>First Data Keys</th>
                    <th>First Data Values</th>
                    <th>Second Data Values</th>
                    <th>Third Data Values</th>
                    <th>Fourth Data Values</th>

                </tr>
            </thead>
            <tbody>


                <?
                    pretty_dump ($result);
                ?>




            </tbody>
        </table>

此代码获取数据源,将它们及其键打印到表中,并将它们与 colors 值的差异进行比较。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM