2

I'm trying to output these arrays in a 3x4 table. At the moment it only outputs the first value of every array in a new row in a 1 column array. While it should be 3 columns with 'qualitypoint','quality' and 'q' next to eachother. And then in a new row 'technologies' , 'tech' and 't' next to eachother etc.

Output screenshot

<?php 
$Array1 = array('qualitypoint', 'technologies', 'India','Hey');
$Array2 = array('quality', 'tech', 'Ind','He');
$Array3 = array('q', 't', 'I','H');
echo '<table border = "3px">';
echo '<tr><td>' . implode('</tr><tr>', $Array1).'</td></tr>';
echo '<tr><td>' . implode('</tr><tr>', $Array2).'</td></tr>';
echo '<tr><td>' . implode('</tr><tr>', $Array3).'</td></tr>';
echo '</table>';
?>
1
  • can you show what is your expected outcome you want? its really confusing what you want? Commented Feb 4, 2016 at 16:42

3 Answers 3

5

implode('</td><td>', $Array1)

implode('</td><td>', $Array2)

implode('</td><td>', $Array3)

Guess this is what you wanted to do... To use td instead of tr

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

3 Comments

I need a 3x4 table not 4x3, 'qualitypoint','quality' and 'q' should be next to eachother in their own column. Thank you anyways though.
echo '<tr><td>' . $Array1[0] . '</td><td>' . $Array2[0] . '</td><td>' . $Array3[0] . '</td></tr>'; echo '<tr><td>' . $Array1[1] . '</td><td>' . $Array2[1] . '</td><td>' . $Array3[1] . '</td></tr>'; echo '<tr><td>' . $Array1[2] . '</td><td>' . $Array2[2] . '</td><td>' . $Array3[2] . '</td></tr>'; So this is what you need? You can write that with loops ofc...
Hanky got what I needed, thank you very much for your help anyways!
2

If you want you can also leave the arrays as they are. But the implode cannot be used that way. This will also work for you:

$Array1 = array('qualitypoint', 'technologies', 'India','Hey');
$Array2 = array('quality', 'tech', 'Ind','He');
$Array3 = array('q', 't', 'I','H');

echo '<table border = "3px">';
foreach($Array1 as $key => $value){
echo '<tr><td>' . $Array1[$key] .'</td><td>' . $Array2[$key] .'</td><td>' . $Array3[$key] .'</td></tr>';
}
echo '</table>';

1 Comment

This worked how i wanted it, thank you very much for the help!
1

Forget all that implode mess that will break on the first possible chance. First restructure your arrays and merge them into 1 array.

$Array[1] = array('qualitypoint', 'technologies', 'India','Hey');
$Array[2] = array('quality', 'tech', 'Ind','He');
$Array[3] = array('q', 't', 'I','H');

Then simply do

echo '<table>';
echo '<table>';
for($i=0;$i<=3;$i++)
{
    echo "<tr>";
    for($j=1;$j<=3;$j++)
    {    
     echo "<td>{$Array[$j][$i]}<td>";
    }
    echo "</tr>";    
}
echo '</table>';

Output

table>
<tr>
    <td>qualitypoint<td>
    <td>quality<td>
    <td>q<td>
</tr>
<tr>
    <td>technologies<td>
    <td>tech<td>
    <td>t<td>
</tr>
<tr>
    <td>India<td>
    <td>Ind<td>
    <td>I<td>
</tr>
<tr>
    <td>Hey<td>
    <td>He<td>
    <td>H<td>
</tr>
</table>

Tip You can use count() and not worry about hardcoding the loop's limit. That way you can scale data as much as you want without changing the code

2 Comments

This worked but was again a 4x3 table not 3x4. Thanks anyways.
Alright thank you very much! Will be using this since it's cleaner and easier to change than Werner's answer. Thank you Hanky Panky!

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.