English 中文(简体)
PHP:
原标题:PHP: unusual table from array
  • 时间:2011-11-23 15:46:43
  •  标签:
  • php
  • arrays

证明你有这样的阵列:

Array([0] => Array([first_name] => Stack [last_name] => Overflow)
      [1] => Array([first_name] => Andrew [last_name] => Oliveira)
     );

很容易以下列方式填写表格:

 <th>first name</th>
   <th>last name</th>
<? foreach($info as $key => $data) { ?>
 <tr>
   <td><?= $data[ first_name ] ?> </td>
   <td><?= $data[ last_name ] ?> </td>
 <tr>
 <? } ?>

以上表格如下:

first name || last name
========================
Stack-------|| Overflow
========================
Andrew----|| Oliveira
========================

...... 但是,鉴于同样的情况,你将如何做这样的表格:

person 0 || person 1
========================
Stack-----|| Andrew
========================
Overflow|| Oliveira
========================
问题回答

与此类似?

<?php
// create line with "Person X"
echo  <tr> ;
$s = sizeof ($info);
for ($i=0;$i<$s;++$i) {
  echo  <td>Person  .$i. </td> ;
}
echo  </tr><tr> ;

// create line with first name
foreach ($info as $data) {
  echo  <td> .$data[ first_name ]. </td> ;
}
echo  </tr><tr> ;

// create line with last name
foreach ($info as $data) {
  echo  <td> .$data[ last_name ]. </td> ;
}
echo  </tr> ;
?>

制作两个阵列,一个是头等,另一个是最后名字。 之后,使用<代码>foreach通过这些代码,并压缩表格的第一线和第二行。

foreach($array as $i=>$data){
    $headers .= "<th>Person $i</th>";
    $first_names .= "<td>".$data["first_name"]."</td>";
    $last_names .= "<td>".$data["first_name"]."</td>";
}

echo "<table>
         <tr>$headers</tr>
         <tr>$first_names</tr>
         <tr>$last_names</tr>
      </table>";

你们来了! 仅举一 lo!

是的,实际上非常容易,但使用这种方法:

<?php $COUNT_INFO=count($info); ?>
<th>first name</th>
    <th>last name</th>
    <tr>
        <? for($i=0; $i<$COUNT_INFO; $i++) { ?>
        <td><?= $info[$i][ first_name ] ?> </td>
        <? } ?>
    </tr>
    <tr>
        <? for($i=0; $i<$COUNT_INFO; $i++) { ?>
        <td><?= $info[$i][ last_name ] ?> </td>
        <? } ?>
    <tr>

The answer of @Vitor42 is ok, but if you want something more generic and you are sure that the data is always complete try with:

<table>
<?php
$info = array(
    array( first_name => Stack , last_name => Overflow ),
    array( first_name => Andrew , last_name => Oliveira )
);
$sHtmlHead =  <tr> ;
$arrHtmlBodies = array();
$nSize = sizeOf($info);

for($i=0;$i<$nSize;$i++){
    $sHtmlHead .=  <th>Person  .$i. </th> ;
    $arrTemp = $info[$i];
    $j = 0;
    foreach($arrTemp as $data){
        if(!isset($arrHtmlBodies[$j])) $arrHtmlBodies[$j] =  <tr> ;
        $arrHtmlBodies[$j] .=  <td> .$data. </td> ;
        if($i==($nSize-1)) $arrHtmlBodies[$j] .=  </tr> ;
        $j++;
    }
} 
echo $sHtmlHead; ?></tr>
<?php
echo implode(  ,$arrHtmlBodies); ?></tr>
</table>

如果你无法确定数据是否完整:

<table>
<?php
$info = array(
    array( first_name => Stack , last_name => Overflow ),
    array( first_name => Andrew , last_name => Oliveira ),
    array( first_name => Eli ),
    array( last_name => Astocaza )
);
$sHtmlHead =  <tr> ;
$arrHtmlBodies = array();
$nSize = sizeOf($info);
$arrKeys = array_keys($info[ 0 ]);
$nSizeKeys = sizeOf($arrKeys);
for($i=0;$i<$nSize;$i++){
    $sHtmlHead .=  <th>Person  .$i. </th> ;
    $arrTemp = $info[$i];
    $j = 0;
    for($j=0;$j<$nSizeKeys;$j++){
        if(!isset($arrHtmlBodies[$j])) $arrHtmlBodies[$j] =  <tr> ;
        $arrHtmlBodies[$j] .=  <td> .$info[$i][$arrKeys[$j]]. </td> ;
        if($i==($nSize-1)) $arrHtmlBodies[$j] .=  </tr> ;
    }
} 
echo $sHtmlHead; ?></tr>
<?php
echo implode(  ,$arrHtmlBodies); ?></tr>
</table>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签