Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php从两个数组(foreach外部)获取相同的索引值_Php_Arrays_Foreach - Fatal编程技术网

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

php从两个数组(foreach外部)获取相同的索引值,php,arrays,foreach,Php,Arrays,Foreach,例如,将数组命名为$data\u debit\u transformance Array ( [0] => Array ( [VatReturnRowNumberForDebitTurnover] => 63 [Total] => 0.00 ) [1] => Array ( [VatReturnRowNumberForDebitTurnover] => 64 [Total] =

例如,将数组命名为
$data\u debit\u transformance

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>
$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]
等。我不知道
[]
的数量;可能是
[1]
,也可能是
[30]

试过这样的东西吗

<table><tr>
<td><strong>63</strong></td>
<td>0.00</td>
</tr><tr>
<td><strong>64</strong></td>
<td>44.28</td>
</tr></table>
$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>
更新感谢@user2340218的建议,获得一些解决方案。对于每个
必须使用
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>

64
67
解决方案最终使用的解决方案@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>
$resultvaturnrownumberfordebitturnover=array();
$resultTotal=array();
foreach($i=>$result的数据\借方\营业额){
$resultVatReturnRowNumberForDebitTurnover[]=$result[VATRurnRowNumberForDebitTurnover];
$resultTotal[]=$result[Total];
}
$VatReturnRowNumberForDebitTurnoverModified=array_combine($ResultAtReturnRowNumberForDebitTurnOver,$resultTotal);
62
63
实际上必须接受@Daniel p answer:)但正如我所理解的,不能接受两个答案

你的意思是

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



使用
foreach
如下:

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

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

Array
(
    [63] => 0.00
    [64] => 44.28
)
数组索引是您的
VatReturnRowNumberForDebitTurnover
,值是您的总数

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

构建表格:

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



在末尾添加了
foreach
。获取具有多个
数组
键的表。但是只需要一个表,然后将
移动到
foreach
外部。这不是很明显吗?如果表位于foreach
之外,那么如何使用此代码
。结果总是
0
!php操作结果和html标记是如何连接的?更新后的解决方案确实有效,但请注意,如果数组变大,并且有大量调用,这可能会占用大量CPU,因为整个数组都是反复运行的。如果你的项目很小,我看不出有什么问题。是的。。。。看来它管用。不要以为只有
需要在
foreach
之外。我将再次测试。根据您的建议,获得一些解决方案(更新后发布)。可能有更好的解决方案……据我所知,我需要将
VatReturnRowNumberForDebitTurnover
转换为
[63][64]
等,然后在必要时
必须使用
if(isset($array[63]){echo$array[63]}
?并且没有任何
foreach
?这将是更好的解决办法。我查一下。数字63、64等需要“手写”,而不是用脚本。而不是
需要是“不可更改”的数字:63、64、67等。以这种方式将数据放入数组的优点是,每次要查找值时,不需要使用
foreach()
遍历整个数组。请注意,
VatReturnRowNumberForDebitTurnover
必须是唯一的(我猜就是这种情况)。您可以使用
if(isset($array[63])
查找单个值,或者使用
foreach()显示整个内容。
将全部选中。我想我可以得到/改变
VatReturnRowNumberForDebitTurnover
是唯一的。是的,
(isset($array[])
会更好
<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>