2

Have array named for example $data_debit_turnover

Array
(
[0] => Array
    (
        [VatReturnRowNumberForDebitTurnover] => 63
        [Total] => 0.00
    )

[1] => Array
    (
        [VatReturnRowNumberForDebitTurnover] => 64
        [Total] => 44.28
    )
)

Have HTML that need to look like this

<table><tr>
<td><strong>63</strong></td>
<td>0.00</td>
</tr><tr>
<td><strong>64</strong></td>
<td>44.28</td>
</tr></table>

At first tried with php foreach, but in such case instead of one table get multiple tables [0], [1] etc.

Then tried

<td><strong>64</strong></td>
<td><?php
if( $data_debit_turnover[1][VatReturnRowNumberForDebitTurnover] == '64'){
echo $data_debit_turnover[1][Total]. ' Total<br>';
}?>
</td>

but problem is with [1] etc. I do not know number of []; may be [1] and may be [30].

Tried something like this

$resultVatReturnRowNumberForDebitTurnover = array();
$resultTotal = array();
foreach($data_debit_turnover as $i => $result){
$resultVatReturnRowNumberForDebitTurnover[] = $result[VatReturnRowNumberForDebitTurnover];
$resultTotal[] = $result[Total];
}

<td><strong>64</strong></td>
<td><?php
if (in_array('64', $resultVatReturnRowNumberForDebitTurnover)) {
echo $resultTotal[1];
}?>
</td>

The same problem [1]. How to echo corresponding (index) value from the another array.

For example if 64 is the second value in array $resultVatReturnRowNumberForDebitTurnover then need to echo the second value from array $resultTotal.

Possibly there is some other way.

Please advice.

Showing what I did with foreach

<?php
foreach($data_debit_turnover as $i => $result){
?>
<table>
<tr>
<td><strong>63</strong></td>
<td>
<?php if($result[VatReturnRowNumberForDebitTurnover] = '63') {
echo $result[Total];
} ?>
</td>
</tr>
<tr>
<td><strong>64</strong></td>
<td>
<?php if($result[VatReturnRowNumberForDebitTurnover] = '64') {
echo $result[Total];
} ?>
</td>
</tr>
</table>
<?php
}
?>

Update Thanks to @user2340218 advice get some solution. For each <td> must use foreach. Possibly there is some better solution.

<table><tr>
<td><strong>64</strong></td>
<td><?php foreach($data_debit_turnover as $vatReturn){if($vatReturn['VatReturnRowNumberForDebitTurnover'] == '64') {echo $vatReturn['Total'];}}?></td>
</tr><tr>
<td><strong>67</strong></td>
<td><?php foreach($data_debit_turnover as $vatReturn){if($vatReturn['VatReturnRowNumberForDebitTurnover'] == '67') {echo $vatReturn['Total'];}}?></td>
</tr></table>

Solution Finally used solution @Daniel P advised.

$resultVatReturnRowNumberForDebitTurnover = array();
$resultTotal = array();
foreach($data_debit_turnover as $i => $result){
$resultVatReturnRowNumberForDebitTurnover[] = $result[VatReturnRowNumberForDebitTurnover];
$resultTotal[] = $result[Total];
}

$VatReturnRowNumberForDebitTurnoverModified = array_combine($resultVatReturnRowNumberForDebitTurnover, $resultTotal);


<table>
<tr>
<td><strong>62</strong></td>
<td>
<?php echo $VatReturnRowNumberForDebitTurnoverModified[62]; ?>
</td>
</tr>
<tr>
<td><strong>63</strong></td>
<td>
<?php echo $VatReturnRowNumberForDebitTurnoverModified[63]; ?>
</td>
</tr>
</table>

Actually have to accept @Daniel P answer:) But as understand can not accept 2 answers

7
  • Added foreach at the end. Get so many tables as have number of array keys. But need only one table Commented Jul 27, 2013 at 14:01
  • Then move <table> outside foreach. Isn't it obvious? Commented Jul 27, 2013 at 14:02
  • If table outside foreach then how can I use this code <?php if($result[VatReturnRowNumberForDebitTurnover] = '64') {echo $result[Total];} ?>. Result always would be 0! Commented Jul 27, 2013 at 14:04
  • How does php operation result and html-tag are connected? Commented Jul 27, 2013 at 14:07
  • 1
    The updated solution does indeed work but beware, if you array gets large and you have lots of calls this might become CPU intensive because the whole array is walked over and over. If your projects is rather small I don't see any problems with that. Commented Jul 27, 2013 at 14:45

3 Answers 3

4

I am guessing that VatReturnRowNumberForDebitTurnover numbers are unique so your array should look like this :

Array
(
    [63] => 0.00
    [64] => 44.28
)

The array index is your VatReturnRowNumberForDebitTurnover and the value is your total.

To test if a VatReturnRowNumberForDebitTurnover has a value your simply use isset()

if (isset($array[63])) {
    echo $array[63]
}

Building the table :

<table>
<?php foreach($data_debit_turnover as $i => $total){ ?>
    <tr>
        <td><strong><?php echo $i; ?></strong></td>
        <td><?php echo $total; ?></td>
    </tr>
<?php } ?>
</table>
Sign up to request clarification or add additional context in comments.

3 Comments

As I understand I need to convert VatReturnRowNumberForDebitTurnover to [63] [64] etc. And then in necessary <td> must use if (isset($array[63])) {echo $array[63]}? And no foreach? It would be better solution. I will check. Numbers 63,64 etc. need to be written "by hand" not by script. Instead of <?php echo $i; ?> need to be "unchangeable" number: 63, 64, 67 etc.
The advantage of putting your data in a array this way is that you do not need to walk the whole array with foreach() each time you wanna look up a value. beware that VatReturnRowNumberForDebitTurnover has to be unique (which I am guess is the case). You can lookup a single value with if (isset($array[63])) or display the whole content with foreach()
Will check all. I think I can get/change that VatReturnRowNumberForDebitTurnover is unique. Yes, (isset($array[])) would be better
3

Do you mean this?

<table>
<?php foreach($data_debit_turnover as $vatReturn){ ?>
     <tr>
        <td><strong><?php print $vatReturn['VatReturnRowNumberForDebitTurnover'] ?></strong></td>
        <td><?php print $vatReturn['total'] ?></td>
     </tr>
<?php } ?>
</table>

2 Comments

Yes.... seems it works. Have not think that only <table> need to be outside foreach. I will test again.
Based on your advice get some solution (posted in question after Update). Possibly there some better solution....
1

Use foreach like this:

<table>
<?php
foreach ($Array as $item)
{
   echo '<tr>';
   echo '<td><strong>' . $item['VatReturnRowNumberForDebitTurnover'] . '</strong></td>';
   echo '<td>' . $item['Total'] . '</td>';
   echo '</tr>'
}
?>
</table>

Comments

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.