[英]what is the correct way to access this multidimensional array with PHP
I cannot seem to find the correct way to access the VAT value of this array...我似乎无法找到访问此数组的增值税值的正确方法...
$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')
);
In my limited knowledge i've tried:以我有限的知识,我尝试过:
$freeholdPurchases['up to £150,000'][1][0]
and lots of variations of this but cant seem to figure it out和很多变化,但似乎无法弄清楚
can anyone help me understand the syntax i need to access it or if i need to change my array at all.任何人都可以帮助我理解访问它所需的语法,或者我是否需要更改我的数组。
Thanks谢谢
This will work for you:这对你有用:
$freeholdPurchases['up to £150,000']['Total'];
You have defined $freeholdPurchases['up to £150,000']
as a tagged (associative) array whose elements have names (tags) so you access its elements by tag name.您已将$freeholdPurchases['up to £150,000']
为一个标记(关联)数组,其元素具有名称(标记),因此您可以通过标记名称访问其元素。 The same is true for $freeholdPurchases
. $freeholdPurchases
也是如此。
Associative array contains keys and values.关联数组包含键和值。 Syntax understanding:语法理解:
$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'];
The question was to access the VAT value of this array.问题是访问这个数组的增值税值。 You will get VAT with the value of £ 148.33 with您将获得价值 148.33 英镑的增值税
$vat148p33 = $freeholdPurchases['£250,001 - £300,000']['VAT'];
All values and the area:所有值和面积:
foreach($freeholdPurchases as $area => $row){
echo 'Area: '.$area.' VAT:'.$row['VAT'].'<br>';
}
Output:输出:
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
This worked perfectly:这工作得很好:
$freeholdPurchases['£250,001 - £300,000']['VAT'] $freeholdPurchases['£250,001 - £300,000']['增值税']
and all the explanations were very helpful too.所有的解释也非常有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.