Jump to content

Array Issue, Need To Insert New Array Data. Please Help!


work_it_work

Recommended Posts

i have an website (shopping-cart) and i want to modify the invoice to show the VAT (tax) .

right now if i enable vat tax the prices are shown incorrectly. if the price added by admin is 1000$ the total price with vat is 1200$

i want the price to be displayed 1000$ and before total i want to display the vat of the invoice example:

if the total is 1000 the vat shown will be 1000 / 20 * 120 which means 166.66.

so the invoice looks like this:

product a qty1 price 1000

subtotal 1000

vat(tax) 166

total to pay 1000

 

in the template file i have this function which generates the total and subtotal:

 <table id="total">
 <?php foreach ($totals as $total) { ?>
 <tr>
 <td class="right"><b><?php echo $total['title']; ?>:</b></td>
 <td class="right"><?php echo $total['text']; ?></td>
 </tr>
 <?php } ?>
</table>

 

using

print_r($totals)

will print:

 

Array (
[0] => Array ( [code] => sub_total [title] => Sub-Total [text] => 1000.00€ [value] => 1000 [sort_order] => 1 )
[1] => Array ( [code] => total [title] => Total [text] => 1000.00€ [value] => 1000 [sort_order] => 9 ) )

 

using

print_r($total)

will print:

Array ( [code] => total [title] => Total [text] => 202.00€ [value] => 202 [sort_order] => 9 )

 

basically it's simple what i want to do

 

$totals array i must insert a new row inside to make it look like this:

Array (
[0] => Array ( [code] => sub_total [title] => Sub-Total [text] => 1000.00€ [value] => 1000 [sort_order] => 1 )
[1] => Array ( [code] => VAT [title] => VAT [text] => 166.66€ [value] => 166.66[sort_order] => 2 )
[2] => Array ( [code] => total [title] => Total [text] => 1000.00€ [value] => 1000 [sort_order] => 9 ) )

 

how can i explode and add a row in the middle of the array?

 

please help, thank you!

Ok, I don't know if I really understand, so I give you a little example how to add a key in the middle of an array

 

 

echo '<pre>';

$a = array(0 => 'A', 1 => 'C');

echo 'BEFORE<br />';
print_r($a);

array_splice($a, 1, 0, 'B');

echo 'AFTER<br />';
print_r($a);

/*
Result:

BEFORE
Array
(
[0] => A
[1] => C
)
AFTER
Array
(
[0] => A
[1] => B
[2] => C
)
*/

thanks for your reply, i managed to solve this problem, here's the solution:

$vat= array(array('code' => 'iva', 'title' => 'IVA', 'text' => $total1, 'value' => '123', 'sort_order' => '2'));
array_splice($totals, 2, 0, $vat);
<table id="total">
	 <?php foreach ($totals as $total) { ?>
	 <tr>
	 <td class="right"><b><?php echo $total['title']; ?>:</b></td>
	 <td class="right"><?php echo $total['text']; ?></td>
	 </tr>
	 <?php } ?>
</table>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.