user2465936
user2465936

Reputation: 1040

php get the same index values from two arrays (outside foreach)

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

Upvotes: 2

Views: 1399

Answers (3)

Daniel P
Daniel P

Reputation: 461

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>

Upvotes: 4

Zeus Alexander
Zeus Alexander

Reputation: 563

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>

Upvotes: 1

ahPo
ahPo

Reputation: 384

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>

Upvotes: 3

Related Questions