1

I have an existing data array with a format like this:

Array ( [0] => Array ( [employee_id] => 14100001 [salary] => 346.35416666667 [late_duration] => 2 [undertime_duration] => 3 [cola_duration] => 0 [date] => 2019-09-09 )
     [1] => Array ( [employee_id] => 14100001 [salary] => 350 [late_duration] => 0 [undertime_duration] => 0 [cola_duration] => 0 [date] => 2019-09-10 )
     [2] => Array ( [employee_id] => 14100002 [salary] => 350 [late_duration] => 0 [undertime_duration] => 0 [cola_duration] => 0 [date] => 2019-09-09 )
     [3] => Array ( [employee_id] => 14100003 [salary] => 358.75 [late_duration] => 0 [undertime_duration] => 0 [cola_duration] => 2 [date] => 2019-09-09 )

now i want to insert a data inside of my existing array to be like this:

Array ( [0] => Array ( [employee_id] => 14100001 [salary] => 346.35416666667 [late_duration] => 2 [undertime_duration] => 3 [cola_duration] => 0 [date] => 2019-09-09 [calculate_id] => 1 )
[1] => Array ( [employee_id] => 14100001 [salary] => 350 [late_duration] => 0 [undertime_duration] => 0 [cola_duration] => 0 [date] => 2019-09-10 [calculate_id] => 1 )
[2] => Array ( [employee_id] => 14100002 [salary] => 350 [late_duration] => 0 [undertime_duration] => 0 [cola_duration] => 0 [date] => 2019-09-09 [calculate_id] => 1 )
[3] => Array ( [employee_id] => 14100003 [salary] => 358.75 [late_duration] => 0 [undertime_duration] => 0 [cola_duration] => 2 [date] => 2019-09-09 [calculate_id] => 1 )

Here is my code and it returns error: Attempt to assign property 'calculate_id' of non-object

$calculate = $this->payroll->record_calculate_date($start_date,$end_date);

            foreach ($this->data_array as $key => $value) {
                array_push($this->data_array , $this->data_array->calculate_id = $calculate);
            }
            foreach ($$this->data_array1 as $key => $value) {
                array_push($this->data_array , $this->data_array->calculate_id = $calculate);
            }
Jc John
  • 1,799
  • 2
  • 34
  • 69
  • So I guess your question is "How do I add an "Insert_ID column to my table?" Suggestion: consider something like [alter table add column insert_id int auto_increment](https://stackoverflow.com/questions/9070764) – paulsm4 Sep 23 '19 at 04:43
  • @paulsm4 i'll edit mo question to be more specific – Jc John Sep 23 '19 at 04:44
  • @paulsm4 please check my editted question – Jc John Sep 23 '19 at 04:47
  • is your `employee_id` autoincremented and primary or not? is `not` then just use `$this->db->insert($data)` query to save data – PHP Ninja Sep 23 '19 at 04:51
  • Q: So what exactly *IS* your question? Q: What happens when you execute the code in your last snippet? If your table doesn't already have "insert_id", I imagine you get a MySQL error. Can you post the error? Q: If it does, why not just `alter table` and make the column auto_increment (s Gulshan suggests)? – paulsm4 Sep 23 '19 at 04:52
  • @paulsm4 please check my data array. something like i want to alter my array and add each row in my array the insert_id of the last inserted data that i've made – Jc John Sep 23 '19 at 05:01
  • @paulsm4 i've edited my whole question please take a look – Jc John Sep 23 '19 at 05:38

1 Answers1

2

You can use array_map

$calculate = $this->payroll->record_calculate_date($start_date,$end_date);
$f = array_map(function($v) use ($calculate){
    return $v + ["calculate_id" => $calculate];
}, $this->data_array);
$this->data_array = $f;

DEMO : https://3v4l.org/0pJNW

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
  • 1
    very great answer. this is what i need – Jc John Sep 23 '19 at 06:29
  • 1
    @Jc John - thank you for clarifying your question. Notes: 1) it always helps to *ASK A SPECIFIC QUESTON*. State your question: don't imply it. 2) Example code is great. 3) Copying/pasting the actual error message related to the code is even better. Lesson learned for your next question :) Please upvote Rakesh Jakhar if he helped; and please "accept" his answer if it resolved the problem. – paulsm4 Sep 23 '19 at 06:37
  • @paulsm4 noted. – Jc John Sep 23 '19 at 06:41