簡體   English   中英

用 PHP 訪問這個多維數組的正確方法是什么

[英]what is the correct way to access this multidimensional array with PHP

我似乎無法找到訪問此數組的增值稅值的正確方法...

$freeholdPurchases = array(
            'up to £150,000' => array('costs' => '£620.83', 'VAT' => '£124.17' , 'Total' => '£745.00'),
            '£150,000 - £200,000' => array('costs' => '£620.83', 'VAT' => '£124.17', 'Total' => '£745.00'),
            '£200,001 - £250,000' => array('costs' => '£700.00', 'VAT' => '£140.00', 'Total' => '£840.00'),
            '£250,001 - £300,000' => array('costs' => '£741.67', 'VAT' => '£148.33', 'Total' => '£890.00'),
            '£300,001 - £400,000' => array('costs' => '£783.33', 'VAT' => '£156.67', 'Total' => '£940.00'),
            '£400,001 - £500,000' => array('costs' => '£825.00', 'VAT' => '£165.00', 'Total' => '£990.00'),
            '£500,001 - £600,000' => array('costs' => '£866.67', 'VAT' => '£173.33', 'Total' => '£1,040.00'),
            '£600,001 to £700,000' => array('costs' => '£908.33', 'VAT' => '£181.67', 'Total' => '£1,090.00'),
            '£700,001 - £800,000' => array('costs' => '£950.00', 'VAT' => '£190.00', 'Total' => '£1,140.00'),
            '£800,001 - £900,000' => array('costs' => '£991.67', 'VAT' => '£198.33', 'Total' => '£1,190.00'),
            '£900,001 to £1,000,000' => array('costs' => '£1,033.33', 'VAT' => '£206.67', 'Total' => '£1,240.00')
    );

以我有限的知識,我嘗試過:

 $freeholdPurchases['up to £150,000'][1][0]

和很多變化,但似乎無法弄清楚

任何人都可以幫助我理解訪問它所需的語法,或者我是否需要更改我的數組。

謝謝

這對你有用:

$freeholdPurchases['up to £150,000']['Total'];

您已將$freeholdPurchases['up to £150,000']為一個標記(關聯)數組,其元素具有名稱(標記),因此您可以通過標記名稱訪問其元素。 $freeholdPurchases也是如此。

關聯數組包含鍵和值。 語法理解:

$data = array(
  'country' => array(
    'company' => array (
      'director' => array(
        'first_name' => 'Bill',
        'last_name' => 'Gates',
      )
    )
  )
)

$director = $data['country']['company']['director'];
$director_full_name = $director['first_name'] . ' ' . $director['last_name'];

問題是訪問這個數組的增值稅值。 您將獲得價值 148.33 英鎊的增值稅

$vat148p33 = $freeholdPurchases['£250,001 - £300,000']['VAT'];

所有值和面積:

foreach($freeholdPurchases as $area => $row){
  echo 'Area: '.$area.' VAT:'.$row['VAT'].'<br>';
}

輸出:

Area: up to £150,000 VAT:£124.17
Area: £150,000 - £200,000 VAT:£124.17
Area: £200,001 - £250,000 VAT:£140.00
Area: £250,001 - £300,000 VAT:£148.33
Area: £300,001 - £400,000 VAT:£156.67
Area: £400,001 - £500,000 VAT:£165.00
Area: £500,001 - £600,000 VAT:£173.33
Area: £600,001 to £700,000 VAT:£181.67
Area: £700,001 - £800,000 VAT:£190.00
Area: £800,001 - £900,000 VAT:£198.33
Area: £900,001 to £1,000,000 VAT:£206.67

這工作得很好:

$freeholdPurchases['£250,001 - £300,000']['增值稅']

所有的解釋也非常有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM