在表中显示数组值

时间:2015-05-13 09:33:19

标签: php html arrays curl html-table

我有以下代码,目前分别在resArrresArr2中显示数组值。我想在表格中显示这些数据,显示resArr中数组column 1的值和resArr2column 2的值。我尝试过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";
       }
   }
}

3 个答案:

答案 0 :(得分:1)

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

&#13;
&#13;
$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>";
&#13;
&#13;
&#13;

答案 1 :(得分:1)

查看为我显示的结果。我正在使用样本数组。

&#13;
&#13;
$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>";
&#13;
&#13;
&#13;

Here is the result generated by the above code

答案 2 :(得分:1)

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

$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样式。