Meet Patel
Meet Patel

Reputation: 105

How can I pass object in array in php?

I am developing php API. I am creating emi_loan_date array from string. by doing this

"emi_date1" => explode(',', $row['emi_loan_date']),

which is look like this

"emi_date1": [
                "23-10-2019",
                "23-11-2019",
                "23-12-2019",
                "23-01-2020",
                "23-02-2020",
                "23-03-2020",
                "23-04-2020",
                "23-05-2020",
                "23-06-2020",
                "23-07-2020",
                "23-08-2020",
                "23-09-2020",
                "23-10-2020"
            ],


Now I want to pass other value named emi_amount in emi_date1. This would be look like this


"emi_date1": [
              {
               date: "23-10-2019",
               emi_amount: 2000
              },
              {
               date: "23-11-2019",
               emi_amount: 2000
              }
            ],



I am getting emi_amount in $row['emi_amount']

Upvotes: 2

Views: 117

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

A simple foreach loop should do the trick (this does assume it's the same $row['emi_amount'] for all entries)...

$output = [];
foreach ( explode(',', $row['emi_loan_date']) as $date )    {
    $output[] = ['date' => $date, 'emi_amount' => $row['emi_amount']];
}
print_r($output);

One thing I would point out is that storing a comma separated list of items in a SQL column is generally a bad idea.

Upvotes: 3

Related Questions