在 table 中显示数组值

Display array values in a table

我有以下代码,目前分别显示 resArrresArr2 中的数组值。我想在 table 中显示此数据,在 column 1 中显示数组 resArr 的值,在 column 2 中显示 resArr2 的值。我曾尝试使用 trtd 进行试验,但是由于某种原因这对输出没有影响。

谁能给我提供任何可能的解决方案?

$resArr = json_decode($res, 1);
$resArr2 = json_decode($res2, 1);
foreach ($resArr as $key => $value)
{
    echo "<tr>";
   if(!is_array($value))     
   {
       if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
        {
            echo "<td>";
            echo  $key . ':' . $value;
            echo "<br />\n";
            echo "</td>";
        }
   }
   echo "</tr>";
}

foreach ($resArr2 as $key => $value)
 {
   //to eliminate array to string conversion error 
   if(!is_array($value))     
   {
       if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
       {
           echo  $key . ':' . $value;
           echo "<br />\n";
       }
   }
}

你必须同时迭代两个数组。试试这个

$resArr = json_decode($res, 1);
$resArr2 = json_decode($res2, 1);
$count1=0;

echo "<table border='1'>";
foreach ($resArr as $key => $value)
{
 echo "<tr>";
   if(!is_array($value))     
   {
    if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
  {
   echo "<td>";
   echo  $key . ':' . $value;
   echo "<br />\n";
   echo "</td>";
  }
   }
   
   $count2=0;
   foreach ($resArr2 as $key => $value)
   {
    if($count1==$count2){
   //to eliminate array to string conversion error 
   if(!is_array($value))     
   {
    if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
    {
     echo "<td>";
     echo  $key . ':' . $value;
     echo "<br />\n";
     echo "</td>";
    }
   }
    }
    $count2++;
  }
   echo "</tr>";
   $count1++;
}
echo "</table>";

看看为我显示的结果。我正在使用示例数组。

$resArr = array('attire'=>'1','category'=>'2','location'=>'3');
$resArr2 = array('attire'=>'a','category'=>'b','location'=>'c');

echo "<table border='1'>";

$count1=0;
foreach ($resArr as $key => $value)
{
 echo "<tr>";
   if(!is_array($value))     
   {
    if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
  {
   echo "<td>";
   echo  $key . ':' . $value;
   echo "<br />\n";
   echo "</td>";
  }
   }
   
   $count2=0;
   foreach ($resArr2 as $key => $value)
   {
    if($count1==$count2){
   //to eliminate array to string conversion error 
   if(!is_array($value))     
   {
    if($key == 'attire' or $key == 'category' or $key == 'location' or $key == 'name' or $key == 'website' or $key == 'checkins' or $key == 'likes')
    {
     echo "<td>";
     echo  $key . ':' . $value;
     echo "<br />\n";
     echo "</td>";
    }
   }
    }
    $count2++;
  }
   echo "</tr>";
   $count1++;
}
echo "</table>";

我相信你想要这样的东西:

$resArr = json_decode($res, 1);
$resArr2 = json_decode($res2, 1);
$labels = array('attire','category','location','name','website','checkins','likes');
$count1=0;

echo '<table border="1">';
echo '<tr><th>key</th><th>resArrLabel</th><th>resArr2Label</th></tr>';
foreach ($labels as $label)
{
    echo '<tr>';
        echo '<th>'.$label.'</th>';
        echo '<td>'.(array_key_exists($label, $resArr) ? $resArr[$label] : '').'</td>';
        echo '<td>'.(array_key_exists($label, $resArr2) ? $resArr2[$label] : '').'</td>';
    echo '</tr>';
}
echo '</table>';

速度要快得多,因为只使用一个循环并将标签添加到每一列和每一行。尝试添加更多 css 样式。