gpt4 book ai didi

php从两个数组中获取相同的索引值(在foreach之外)

转载 作者:行者123 更新时间:2023-12-02 07:02:36 29 4
gpt4 key购买 nike

将数组命名为例如 $data_debit_turnover

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

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

HTML需要看起来像这样

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

起初尝试使用 php foreach , 但在这种情况下,不是一张表而是多张表 [0], [1]

然后尝试

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

但问题在于 [1]等等 我不知道 [] 的数量;可能是 [1]并且可能是 [30] .

试过这样的东西

$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>

同样的问题[1] .如何从另一个数组中回显相应的(索引)值。

例如如果64是数组 $resultVatReturnRowNumberForDebitTurnover 中的第二个值然后需要回显数组 $resultTotal 中的第二个值.

可能还有其他方法。

请指教。

显示我用 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
}
?>

更新 感谢@user2340218 的建议得到一些解决方案。对于每个 <td>必须使用 foreach .可能有一些更好的解决方案。

<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>

解决方案 最后使用@Daniel P 建议的解决方案。

$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>

其实不得不接受@Daniel P 的回答:)但是据了解不能接受2个回答

最佳答案

我猜测 VatReturnRowNumberForDebitTurnover 数字是唯一的,因此您的数组应该如下所示:

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

数组索引是您的 VatReturnRowNumberForDebitTurnover,值是您的总数。

要测试 VatReturnRowNumberForDebitTurnover 是否有值,您只需使用 isset()

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

建表:

<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>

关于php从两个数组中获取相同的索引值(在foreach之外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17898520/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com