如何在关联数组中使用array_push

时间:2017-08-04 11:55:10

标签: php arrays

我现在有一个像这样的数组:

      $this->array[$i] = array(
                  'Date' => $date->format('d/m/Y'),
                  'Capital' => $capital,
                  'Rate' => $rate,
                  'Interest' => $interest,
                  'Payment' => $payment,
                  'Amortization' => $amortization,
                  'Remaining' => $remaining,
                  'InterestTotal' => $interestTotal,
                  'AmortizationTotal' => $amortizationTotal,
                  'PaymentTotal' => $paymentTotal,
                  'InverseCapital' => $inverseCapital,
              );

所以...在这篇文章中,$ i = 0 ... $ i< 360 ... 它将这些值推送到数组中。

我想让这部分代码独立于[$ i]。我希望它像是一样:

array_push(($this->array),
                  'Date' => $date->format('d/m/Y'),
                  'Capital' => $capital,
                  'Rate' => $rate,
                  'Interest' => $interest,
                  'Payment' => $payment,
                  'Amortization' => $amortization,
                  'Remaining' => $remaining,
                  'InterestTotal' => $interestTotal,
                  'AmortizationTotal' => $amortizationTotal,
                  'PaymentTotal' => $paymentTotal,
                  'InverseCapital' => $inverseCapital,
);

2 个答案:

答案 0 :(得分:0)

Just push complete data as an array:-

array_push($this->array,array(
      'Date' => $date->format('d/m/Y'),
      'Capital' => $capital,
      'Rate' => $rate,
      'Interest' => $interest,
      'Payment' => $payment,
      'Amortization' => $amortization,
      'Remaining' => $remaining,
      'InterestTotal' => $interestTotal,
      'AmortizationTotal' => $amortizationTotal,
      'PaymentTotal' => $paymentTotal,
      'InverseCapital' => $inverseCapital
     )
);

Or do like below:-

$this->array['Date'] = $date->format('d/m/Y');
$this->array['Capital'] = $capital;
$this->array['Rate'] = $rate;
$this->array['Interest'] = $interest;
$this->array['Payment'] = $payment;
$this->array['Amortization'] = $amortization;
$this->array['Remaining'] = $remaining;
$this->array['InterestTotal'] = $interestTotal;
$this->array['AmortizationTotal'] = $amortizationTotal;
$this->array['PaymentTotal'] = $paymentTotal;
$this->array['InverseCapital'] = $inverseCapital;

Note:- first-one will give you multi-dimensional array, while second-one will give you single-dimensional array.

答案 1 :(得分:-1)

You are using an associative array so you just set the key/value pair like this.

$array["Password"] = pass;

I think you may need to review the difference between an array and an associative array. For example if I ran the same command again with a different value it would overwrite the old one:

$array["Password"] = "overwritten";

Giving you this

Array ( [Username] => user 
        [Email] => email
        [Password] => "overwritten"
      )

Which judging by your question is not what your expecting

OR

Try out array_merge instead:

$array = array('Username' => 'user', 'Email' => 'email'); 
$array = array_merge($array, array('Password' => 'pass'));

This produces the array:

array('Username' => 'user', 'Email' => 'email', 'Password' => 'pass');