0

I have the following code, which currently displays the array values in resArr and resArr2 separately. I would like to display this data in a table, showing the value of array resArr in column 1 and the value of resArr2 in column 2. I have tried experimenting with tr and td however for some reason this has no effect on the output.

Could anyone provide me with any possible solution please?

$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";
       }
   }
}
2
  • You have to iterate through both arrays at the same time: One row(<tr>) will be created at each iteration and you'll add a new column in your loop when going from Arr1 to Arr2 Commented May 13, 2015 at 9:46
  • can you show your expected output screenshot or a glimpse of result hers? Commented May 13, 2015 at 10:10

3 Answers 3

1

I belive you want something like this:

$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>';

It was much faster becouse use only one loop and add th label to each column and row. Try to add more css style.

Sign up to request clarification or add additional context in comments.

9 Comments

The table will still not appear using this method.
so try: $resArr = json_decode($res, 1); $resArr2 = json_decode($res2, 1); var_dump($retArr); var_dump($retArr2); die(); and copy result into comment.
array(23) { ["id"]=> string(11) "10936503735" ["about"]=> string(73) "Welcome to asos.do/p3RFnb, where you’ll Discover Fashion Online."["category"]=> string(8) "Clothing" ["checkins"]=> int(2893) ["id"]=> string(17) "10152708202338736" } ["name"]=> string(4) "ASOS" ["parking"]=> array(3) { ["website"]=> string(82) "asos.do/p3RFnb marketplace.asos.com fashionfinder.asos.com" } array(1) { ["error"]=> array(3) { ["message"]=> string(50) "You must use https:// when passing an access token" ["type"]=> string(14) "OAuthException" ["code"]=> int(1) } }
This is only part of the array, had to delete some of it as it did not fit,however it should not make a difference. For some reason the output of resArr2 was not displayed. And I notice that I have an error in the end, however this was not effecting my output when I was not trying to display values in table
You must change something else in your code. Try to resolve error. You get data from url without using https protocol. Add https:// at the begin of your url. Output of resArr2 was not displayed becouse there is nothing to display.
|
1

You have to iterate both arrays at the same time. Try this

$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>";

5 Comments

The arrays are being iterated at the same time, however the table is not being displayed. The values are just being displayed according to the key
can you show your expected output screenshot or a glimpse of result hers?
I would like to have a table with rows and columns showing the key in the first column, the value of the first array in the second column, and the value of the second array in the third column. However no type of table is being displayed, only plain text
just add the table tag before and after the parent foreach loop. hope you will see the expected results.
I added this tag echo "<table>" before and after the foreach loop, however a table is still not being displayed.
1

Look at the results displayed for me. I am using sample array.

$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>";

Here is the result generated by the above code

4 Comments

This is exactly how I would like my code to be shown! I have copied the entire code, however still no table is being shown. Would that mean that the problem is in the array?
Yah, If this code is not working than make sure your array is correct by print_r($resArr); and print_r($resArr); and comment all the code below.
array(23) { ["id"]=> string(11) "10936503735" ["about"]=> string(73) "Welcome to asos.do/p3RFnb, where you’ll Discover Fashion Online."["category"]=> string(8) "Clothing" ["checkins"]=> int(2893) ["id"]=> string(17) "10152708202338736" } ["name"]=> string(4) "ASOS" ["parking"]=> array(3) { ["website"]=> string(82) "asos.do/p3RFnb marketplace.asos.com fashionfinder.asos.com/"; } array(1) { ["error"]=> array(3) { ["message"]=> string(50) "You must use https:// when passing an access token" ["type"]=> string(14) "OAuthException" ["code"]=> int(1) } }
do not dump just $resArr = json_decode($res, 1); print_r($resArr); $resArr2 = json_decode($res2, 1); print_r($resArr); die();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.