-3
    Array (    

    [x0] => sometext1     
    [x1] => sometext2     
    [x2] => sometext3    
    [x3] => sometext4     
    [x4] => sometext5    
    [x5] => sometext6
    [x?] => sometext?

    [y0] => someothertext1     
    [y1] => someothertext2  
    [y2] => someothertext3     
    [y3] => someothertext4     
    [y4] => someothertext5     
    [y5] => someothertext6
    [y?] => someothertext?

    ) 

我正在尝试使用这个数组并创建一个表。基本上我不知道有多少 x 或 y ......到目前为止我找不到任何解决方案。任何帮助将不胜感激。

我试图得到的结果

桌子

   x      |       y
sometext1 | someothertext1
sometext2 | someothertext2
...       | ...
4

4 回答 4

1

如果可能,请对数组进行小修正,说明它是如何声明的。让它看起来像这样:

Array (    
    [x] => Array (
        [0] => sometext1     
        [1] => sometext2     
        [2] => sometext3    
        [3] => sometext4     
        [4] => sometext5    
        [5] => sometext6
        [?] => sometext?
    )
    [y] => Array (
        [0] => sometext1     
        [1] => sometext2     
        [2] => sometext3    
        [3] => sometext4     
        [4] => sometext5    
        [5] => sometext6
        [?] => sometext?
    )
)

并且通过这种方式,使用 PHP,您可以这样处理表格:

<table>
<?php
    echo '<tr>';
    foreach ($array as $heading => $contents)
        echo "<th>$heading</th>";
    echo '</tr>';
    foreach ($array as $heading => $contents)
        foreach ($heading as $value)
            echo "<td>$value</td>";
?>
</table>
于 2013-02-19T07:12:03.133 回答
1

尝试这个 :

    <?php
$array  = Array('x0' => 'sometext1',     
                'x1' => 'sometext2',     
                'x2' => 'sometext3',    
                'x3' => 'sometext4',     
                'x4' => 'sometext5',    
                'x5' => 'sometext',
                'y0' => 'someothertext1',     
                'y1' => 'someothertext2',  
                'y2' => 'someothertext3',     
                'y3' => 'someothertext4',     
                'y4' => 'someothertext5',     
                'y5' => 'someothertext6',
                );

$res    = array();              
foreach($array as $key=>$val){
    preg_match('/(?P<var>\w{1})(?P<ky>\d+)/',$key, $match);
    $res[$match['ky']][$match['var']] = $val;
}


echo "<table>";
echo "<tr><td>X</td><td>Y</td></tr>";
foreach($res as $keys=>$vals){  
    echo "<tr><td>".$vals['x']."</td><td>".$vals['y']."</td></tr>";
}
echo "<table>";

?>

输出 :

X           Y
sometext1   someothertext1
sometext2   someothertext2
sometext3   someothertext3
sometext4   someothertext4
sometext5   someothertext5
sometext    someothertext6
于 2013-02-19T07:16:47.243 回答
0

正如 Praveen Kumar 解释的那样,表格如下所示:

#   X              y
0   sometext1     sometext1
1   sometext2     sometext2
于 2013-02-19T07:15:07.317 回答
0

在本例中,将该数组复制到另一个 var 中,您有两个名称为 $arr 和 $arr2 的数组,它们只是每个数组的副本。

$arr = $arr2 = Array (    .... );

foreach($arr as $key=>$data)
{
    $num = substr($key,1);
    echo '<tr><td>'.$data.'</td>';
    foreach($arr2 as $key2=>$data2)
    {
        $num2 = substr($key2,1);
        if($num == $num2)
        {
            echo '<td>'.$data.'</td>';
            break;
        }
    }
    echo '</tr>';
}

这当然没有考虑到如果没有“y”来匹配额外的“x”,则修复丢失的 td 之类的事情。

于 2013-02-19T07:11:51.817 回答