0

I have an array of 3 companies, which need to be inserted into the db but with 2 additional parameters added to them.

$companyList = [{"name": "apple", "founder": "steve"},
                {"name": "google", "founder": "larry"},
                {"name": "facebook", "founder": "mark"},
               ];

Need to append these 2 parameters for each company (issue is in this step):

$companyListFinal = [];
foreach ($companyList as $company) {
  $companyListFinal[] = array_add($company,['keyAppend1' => 'key 1 appended',
                                            'keyAppend2' => 'key 2 appended'];
}

The final step is to insert the company list with the appended values into the DB:

DB::table('companies')->insert($companyListFinal);

I can't seem to be able to append the 2 new parameters to create the final array to insert:$companyListFinal

What's the correct way to add the parameters to each company so they are all inserted at bulk?

2 Answers 2

1

You need to use array_merge instead of array_add

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, array_merge was the solution.
0

Try using array_push() instead on array_add(). That should do the job.

1 Comment

I tried array_push() instead but still didn't work. When we print this $companyListFinal it should list an array, with 3 objects, each with 4 fields, (the 2 old and the 2 newly appended). I am still unable to print that before entry to db as a check.

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.