0

I am having trouble with expanding my PHP array. Here is a sample of my code :

    <select name="PaymentCounts">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>
<?php
$arr = array ('Client' => "Alex", 'BillNumber' => "123", 'PaymentCounts' => "1", 'Amounts' =>
  array ('PaymentNumber' => 
    array( array('Amount1' => "100.00", 'AmountDate1' => "27.05.2015"))), 'ActiveClient => "1");
echo "<pre>";
    print_r($arr);
echo "</pre>";
?>

This is how it looks like when PaymentsCount is 1. Can someone give me an approach, if Payments count is more than 1 my PHP array could auto expand and more specificly start copying this

   array('Amount1' => "100.00", 'AmountDate1' => "27.05.2015"),
   array('Amount2' => "100.00", 'AmountDate2' => "27.06.2015")

I think there must be combined if, for and push_array but I can't sort it out by myself. I would be really pleased if you also give me a suggestion how to make Amount$i and AmountDate$i index increse by one until it reaches the PaymentsCount

1 Answer 1

1

You may consider using for() and array_push() function.

Schematic code could look like this:

    $paymentsArray = array();
$day = "27";
$month = "05";
$year ="2015";
            for($i=0; $i <= $PaymentCounts; $i++)
            {

        array_push($paymentsArray, array('Amount1' => "100.00", 'AmountDate1' => "$day.$month.$year"));
$month++;
            }
      $arr = array ('Client' => "Alex", 'BillNumber' => "123", 'PaymentCounts' => "1", 'Amounts' => $paymentsArray);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help. I thought array_push adds only in the end of the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.