0

I'm trying to export my array data from our org's API to the table but it doesn't like what my desired result, I'm struggling for hours and almost a day now. Here is my array sample:

Array (
[BASIC INCOME] => Array
(
    [RADIATOR] => 61154
    [METAL FABRICATION] => 41714
)
 [OT PAY] => Array
(
    [RADIATOR] => 35492.58
    [METAL FABRICATION] => 42670.81
)
)

Here is what I've tried now,

$j = 0;

foreach($summer as $key=>$val){

    $r = 0;
    
    foreach(array_keys($summer) as $gl_account){  
         
        ?>
        
    <tr>
        <td><?=array_keys($val)[$j]?></td>
        <td></td>
        <td><?=$gl_account?></td>
        <td><?=$summer[$sum]['BASIC INCOME']?></td>
        <td><?=$summer[$sum]['OT PAY']?></td>
        <td><?=$val[array_keys($val)[$j]]?></td>
        <td><?=$val[array_keys($val)[$j]]?></td>
        <td><?=$summer[$sum]['OT PAY']?></td>
        <td><?=$summer[$sum]['OT PAY']?></td>
        <td><?=$summer[$sum]['OT PAY']?></td>
    </tr>
    <?php $r++;} ?>
<?php $j++; }} ?>

Here is my desired result:

enter image description here

Maybe I have to format this array? In order to do it, I also need to use array manipulation which is also my problem. Thanks for the help...

4
  • 1
    It looks like you should transpose the array first. Then you can loop over it to generate the table you want. Commented May 25, 2023 at 23:17
  • 1
    See here for how to transpose associative arrays. Commented May 25, 2023 at 23:18
  • 1
    Your minimal reproducible example doesn't seem to have a direct relationship between your sample input and your exact desired output. I'm not sure if you need transposition or pivoting or both. Related content: Display PHP multidimensional array in html table with each subarray in a column Commented May 26, 2023 at 0:58
  • 1
    yeahh so hard to format that data into table... Commented May 26, 2023 at 1:35

1 Answer 1

0

I solved my problem with mapping it to table, it's messy but it works,

 $i = 0;
foreach($summer as $key=>$val){
   $j = 0;
   foreach(array_keys($summer) as $gl_account){ ?>
       <tr>
           <td><?=array_keys($val)[$i]?></td>
           <td></td>
           <td><?=$gl_account?></td>
           <td></td>
           <td></td>
           <td><?=number_format($summer[$gl_account][array_keys($val)[$i]] * 0.32, 2)?></td>
           <td><?=number_format($summer[$gl_account][array_keys($val)[$i]] * 0.20, 2)?></td>
           <td><?=number_format($summer[$gl_account][array_keys($val)[$i]] * 0.20, 2)?></td>
           <td><?=number_format($summer[$gl_account][array_keys($val)[$i]] * 0.26, 2)?></td>
           <td><?=number_format($summer[$gl_account][array_keys($val)[$i]] * 0.02, 2)?></td>
     </tr>
   <?php $j++;} ?>
<?php $i++; }} ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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