1

I'm getting all the HTML post values in the $postdata variable.

$postdata = $this->input->post();
unset($postdata['submit']);
unset($postdata['valve_no']);

Output of $postdata:

Array ( 
    [1] => Array ( 
        [0] => BH123 
        [1] => H89 
    ) 
    [2] => Array ( 
        [0] => BH123 
        [1] => H89 
    ) 
    [3] => Array ( 
        [0] => BH123 
        [1] => H89 
    ) 
    [4] => Array ( 
        [0] => BH123 
    )
)


$valve_no=$this->input->post('valve_no');

Output of $valve_no:

Array ( 
    [0] => 1 
    [1] => 2 
    [2] => 3 
    [3] => 4 
)

Next is I'm trying to merge both arrays

foreach($postdata as $key => $val) 
{
    $dataSet[] = array ('valve_no'=>$valve_no[$key-1],$postdata[$key]);
} 
print_r($dataSet);

Output of $dataSet:

Array ( 
    [0] => Array ( 
        [valve_no] => 1 
        [0] => Array ( 
            [0] => BH123 
            [1] => H89 
        ) 
    ) 
    [1] => Array ( 
        [valve_no] => 2 
        [0] => Array ( 
            [0] => BH123 
            [1] => H89 
        ) 
    ) 
    [2] => Array ( 
        [valve_no] => 3 
        [0] => Array ( 
            [0] => BH123 
            [1] => H89 
        ) 
    ) 
    [3] => Array ( 
        [valve_no] => 4 
        [0] => Array ( 
            [0] => BH123 
        ) 
    ) 
)

The output I'm expecting is below:

Array ( 
        [0] => Array ( 
            [valve_no] => 1 
            [1] => BH123 
            [2] => H89 
        ) 
        [1] => Array ( 
            [valve_no] => 2 
            [1] => BH123 
            [2] => H89 
        ) 
        [2] => Array ( 
            [valve_no] => 3 
            [1] => BH123 
            [2] => H89 
        ) 
        [3] => Array ( 
            [valve_no] => 4 
            [1] => BH123 
        ) 
    ) 
)

As you can see in the expected output I want to extract the sub-array and need to start with [1] instead of [0].

Thanks in advance.

1 Answer 1

1

Just change foreach() code like this: (As per your comment)

foreach($postdata as $key => $val) 
{
    
    $dataSet[$key-1]['valve_no'] = $valve_no[$key-1];
    foreach($val as $k=>$v){
        $dataSet[$key-1][$k+1] =$v;
    }
}

print_r($dataSet);

Output: https://3v4l.org/P8NKs

Note: In case $postdata sub-array indexes not start with 0,1,2... and still you want them to start with 1,2,... in your result, then do like below:

foreach($postdata as $key => $val) 
{
    
    $dataSet[$key-1]['valve_no'] = $valve_no[$key-1];
    $srNo = 1;
    foreach($val as $v){
        $dataSet[$key-1][$srNo] =$v;
        $srNo++;
    }
}

Output: https://3v4l.org/sLVv5

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

2 Comments

Thanks for the answer. Let me implement and let you know.
@Hello glad to help you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.