2013-07-27 61 views
2

有無陣列命名例如$data_debit_turnoverPHP從兩個陣列(外的foreach)

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個回答

+0

顯示'foreach'代碼 –

+0

接着說:foreach'底。獲得如此多的表格以及'array'鍵的數量。但只需要一張表 – user2465936

+0

然後將'

'移到'foreach'之外。這不明顯嗎? –

回答

3

你的意思呢?

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

這是一個問題? – Oswald

+0

是的....看起來很有效。不要以爲只有'

'需要在'foreach'之外。我會再試一次。 – user2465936

+0

根據您的建議獲得一些解決方案(更新後發佈問題)。可能有一些更好的解決方案...... – user2465936

1

使用foreach這樣的:

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

我猜測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> 
+0

據我所知,我需要將'VatReturnRowNumberForDebitTurnover'轉換爲'[63] [64]'等。然後在必要的'​​'必須使用'if(isset($ array [63] )){echo $ array [63]}'?沒有「foreach」?這將是更好的解決方案。我會查的。數字63,64等需要「手寫」而不是腳本。而不是'<?php echo $ i; ''需要「不可更改」數字:63,64,67等 – user2465936

+0

以這種方式將數據放入數組中的優點是,您不必每次都用'foreach()'遍歷整個數組想要查看價值。請注意'VatReturnRowNumberForDebitTurnover'必須是唯一的(我猜是這種情況)。你可以用'if(isset($ array [63]))'查找單個值,或者用'foreach()顯示整個內容' –

+0

將檢查所有內容。我想我可以修改'VatReturnRowNumberForDebitTurnover'是唯一的。是的,'(isset($ array []))'會更好 – user2465936