我一直在输出错误的输出,因为每个钱包都有最后的金额,任何人都可以提供帮助吗?非常感谢。
foreach ( $xml->NewDataSet->WalletData as $PGW) {
printf("%s\t%s\n", $PGW->walletID, $PGW->Walletamount);
}
这个foreach输出是:
walletID Walletamount
1 100
4 40
第二个foreach as blow:
foreach ( $xml2->NewDataSet->Provider as $provide) {
if ($provide->WalletType == 'Black Wallet') {
printf("%s\t%s\t%s\n", " ", $provide->WalletID, $provide->WalletName));
}
}
输出是:
amount WalletID WalletName
1 A
2 B
3 C
4 D
5 E
6 F
我如何将这样的两个foreach和输出结合起来。
amount walletname
100.0 A
0.0 B
0.0 C
40.0 D
0.0 E
0.0 F
答案 0 :(得分:1)
我会构建一个数组:
$wallets = array();
foreach ( $xml2->NewDataSet->Provider as $provide) {
if ($provide->WalletType == 'Black Wallet') {
$wallets[$provide->WalletID] = array('name' => $provide->WalletName, 'amount' => 0);
}
}
foreach ( $xml->NewDataSet->WalletData as $PGW) {
$wallets[$PGW->walletID]['amount'] = $PGW->Walletamount;
}
for( $i=0;$i<count($wallets);$i++ ){
printf("%s\t%s\n", $wallets[$i]['amount'], $wallets[$i]['name']);
}
答案 1 :(得分:0)
快速回答是:
foreach ( $xml2->NewDataSet->Provider as $provide) {
if ($provide->WalletType == 'Black Wallet') {
if (isset($xml->NewDataSet->WalletData[$provide->WalletID])) {
printf("%s\t%s\t%s\n", $xml->NewDataSet->WalletData[$provide->WalletID]->Walletamount, $provide->WalletID, $provide->WalletName));
} else {
printf("%s\t%s\t%s\n", 0, $provide->WalletID, $provide->WalletName));
}
}
}
但我不知道您的变量$xml
和$xml2
是什么。
答案 2 :(得分:0)
我还没有开始,因为你还没有提供正常形式的阵列但它必须工作
<?php
$array1=Array ();
foreach ( $xml->NewDataSet->WalletData as $PGW) {
$array1[$PGW->walletID]=$PGW->Walletamount;
}
foreach ( $xml2->NewDataSet->Provider as $provide) {
if ($provide->WalletType == 'Black Wallet') {
if(isset($array1[$provide->WalletID]))
$data=$array1[$provide->WalletID];
else
$data=0;
echo sprintf("%s\t%.1F\n", " ", $data, $provide->WalletName);
}
}
?>