John Davidson

mysql - Convert array to Horizontal in php

0 comments
Message:


I have the following result array in ci3 :


array (size=3)
0 =>
array (size=2)
'Months' => string 'January' (length=7)
'Operations' => string '6' (length=1)
1 =>
array (size=2)
'Months' => string 'February' (length=8)
'Operations' => string '3' (length=1)
2 =>
array (size=2)
'Months' => string 'March' (length=5)
'Operations' => string '7' (length=1)

I want to display the above (or any derived array) in the following horizontal form:


Months     January February March
Operations 6 3 7

Right now I'm using the following code to display the table in Vertical form :


<table>
<thead>
<tr>
<th>Month</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
<?php if ($months): ?>
<?php foreach ($months as $result): ?>
<tr>
<td><?php echo $result['Months']; ?></td>
<td><?php echo $result['Operations']; ?></td>
</td>
</tr>
<?php endforeach;?>
<?php endif;?>
</tbody>
</table>

I have converted my array to this form using array_map :


$converted = array_map(null, ...$months);

array (size=2)
0 =>
array (size=3)
0 => string 'January' (length=7)
1 => string 'February' (length=8)
2 => string 'March' (length=5)
1 =>
array (size=3)
0 => string '6' (length=1)
1 => string '3' (length=1)
2 => string '7' (length=1)

But how to iterate in order to get the desired result ?



My Answer:
You can iterate through the converted array and display the data in a horizontal form like this:

php

















Months
Operations



This code will display the data in a horizontal form with the months in the first row and the corresponding operations in the second row.

Rate this post

5 of 5 based on 7684 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us