3v4l.org

run code in 300+ PHP versions simultaneously
<?php $response = '<ItemTaxWithheldList> <TaxWithheldComponent> <TaxesWithheld> <ChargeComponent> <ChargeType>MarketplaceFacilitatorTax-Shipping</ChargeType> <ChargeAmount> <CurrencyAmount>-5.54</CurrencyAmount> </ChargeAmount> </ChargeComponent> <ChargeComponent> <ChargeType>MarketplaceFacilitatorTax-Principal</ChargeType> <ChargeAmount> <CurrencyAmount>-10.87</CurrencyAmount> </ChargeAmount> </ChargeComponent> </TaxesWithheld> </TaxWithheldComponent> <TaxWithheldComponent> <TaxesWithheld> <ChargeComponent> <ChargeType>MarketplaceFacilitatorTax-Other</ChargeType> <ChargeAmount> <CurrencyAmount>-0.27</CurrencyAmount> </ChargeAmount> </ChargeComponent> <ChargeComponent> <ChargeType>MarketplaceFacilitatorTax-Shipping</ChargeType> <ChargeAmount> <CurrencyAmount>0.0</CurrencyAmount> </ChargeAmount> </ChargeComponent> <ChargeComponent> <ChargeType>MarketplaceFacilitatorTax-Principal</ChargeType> <ChargeAmount> <CurrencyAmount>0.0</CurrencyAmount> </ChargeAmount> </ChargeComponent> </TaxesWithheld> </TaxWithheldComponent> <TaxWithheldComponent> <TaxesWithheld> <ChargeComponent> <ChargeType>MarketplaceFacilitatorTax-Shipping</ChargeType> <ChargeAmount> <CurrencyAmount>0.0</CurrencyAmount> </ChargeAmount> </ChargeComponent> <ChargeComponent> <ChargeType>MarketplaceFacilitatorTax-Principal</ChargeType> <ChargeAmount> <CurrencyAmount>-4.87</CurrencyAmount> </ChargeAmount> </ChargeComponent> </TaxesWithheld> </TaxWithheldComponent> </ItemTaxWithheldList>'; $return_data = simplexml_load_string($response); $array = json_decode(json_encode((array)$return_data), TRUE); $chargeTypes = array('MarketplaceFacilitatorTax-Other', 'MarketplaceFacilitatorTax-Shipping', 'MarketplaceFacilitatorTax-Principal'); foreach ($array['TaxWithheldComponent'] as $itemTaxWithheldList) { $charges = array(); foreach ($itemTaxWithheldList['TaxesWithheld']['ChargeComponent'] as $key) { $mpftChargeType = $key['ChargeType']; $mpftChargeAmount = number_format((float)$key['ChargeAmount']['CurrencyAmount'], 2, '.', ''); $charges[$mpftChargeType] = $mpftChargeAmount; } echo '<tr>'; foreach ($chargeTypes as $chargeType) { echo '<td>' . ($charges[$chargeType] ?? '0.00') . '</td>'; } echo '</tr>'; }

preferences:
25.13 ms | 407 KiB | 5 Q