-6

I am explaining my scenario as

i have form in which adding checkbox row multiple times. in first row checkbox values from Monday to Sunday In second row checkbox values also from monday to sunday. I mean to say every row handling on Add Button. Below is the view.php code

<div id="myDIV1" class="form-group  col-md-3 col-lg-3 col-sm-3 col-xs-12"><label for="day" class="control-label">Day</label><br><label class="checkbox-inline"><input type="checkbox" name="day[]" value="Monday">Monday</label><label class="checkbox-inline"><input type="checkbox" name="day[]" value="Tuesday">Tuesday</label><label class="checkbox-inline"><input type="checkbox" name="day[]" value="Wednesday">Wednesday</label><label class="checkbox-inline"><input type="checkbox" name="day[]" value="Thursday">Thursday</label><label class="checkbox-inline"><input type="checkbox" name="day[]" value="Friday">Friday</label><label class="checkbox-inline"><input type="checkbox" name="day[]" value="Saturday">Saturday</label><label class="checkbox-inline"><input type="checkbox" name="day[]" value="Sunday">Sunday</label><?php echo form_error('day'); ?></div>

Below is the controller code.....

$day = implode(',', $this->input->post('day'));

i am using implode bcoz suppose in the first row i have checked Monday and Sunday so it will give me value as Monday, Sunday

My Scenario is i have checked Monday, Sunday from first row and also checked Tuesday, Wednesday from secound row

According to my code i am getting result as Monday, Sunday, Tuesday, Wednesday in $day But i want to store Monday, Sunday in first row of table and Tuesday, Wednesday in second row of table. I am sharing the image link as below https://drive.google.com/open?id=1k8bYsRUJFo9mI2AY6D-d4MGHEoWudB7q

In the image Add Date & Time is dynamic.

Plz guide me for the same. Thanks

13
  • you can use array_merge Commented Mar 22, 2019 at 8:27
  • 1
    Code, not php version. Commented Mar 22, 2019 at 8:37
  • 1
    3v4l.org/L6cEJ your error is somewhere else. Enable error reporting and check. Commented Mar 22, 2019 at 8:41
  • 1
    @AmodKumar we don't need your scenario, we need exact code which is NOT working. Commented Mar 22, 2019 at 8:45
  • 2
    I think the array posted is created in a loop and he tries to merge after the loop. That is why he has two seperate arrays in the outpiut Commented Mar 22, 2019 at 8:47

4 Answers 4

1

Use array_merge method: http://php.net/manual/en/function.array-merge.php

<?php

$array = array("1" );
$otherArray = array("2");

$result = array_merge($array, $otherArray);

print_r($result);

see live code: http://sandbox.onlinephpfunctions.com/code/7fb9da6de4fa9d0a8b220a06d2a59e9007655df1

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

4 Comments

This is not what op has. Op has one array, not two arrays
He has two arrays
According to OP he has one multidimensional array.
You didn't read correctly. He want a result consist of two items in an array.
0

Surely just array_merge will suffice?

    $a=[14];
    $b=[18];
    $c=[23];
    $d=[44];
    $e=[27];
    $f=[31];
    $g=[99];


    $out=array_merge($a,$b,$c,$d,$e,$f,$g);
    printf('<pre>%s</pre>',print_r($out,true));

This will output:

Array
(
    [0] => 14
    [1] => 18
    [2] => 23
    [3] => 44
    [4] => 27
    [5] => 31
    [6] => 99
)

Comments

0

OP asks about merging arrays from one array. We don't know how many arrays there, so we need to use array_walk_recursive function. Pass $result from public scope by reference(!) in anonymous function and store in it all inner arrays values

$a = [[1], [2, 3]];

$result = [];
array_walk_recursive($a, function($value) use (&$result){
    $result[] = $value;
});
var_dump($result);

Comments

-1

Why call_user_func?

$a = array(14);
$b = array(18);
$merged = array_merge($a,$b);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.