Saturday, January 29, 2022

[FIXED] How to combine one key with array as value?

Issue

I have an array and I want to create a table with that array as a column in codeigniter.

How can I combine data that as another array?

Here are my column names

Array
(
    [0] => System_ID
    [1] => Last Name
    [2] => First Name
    [3] => Full Name
    [4] => Phone
    [5] => Ext
    [6] => Email
    [7] => Dept
    [8] => Site
    [9] => Room
    [10] => Job Title
    [11] => Image
    [12] => URL
    [13] => Active
)

And here is the array that I want to combine as value

Array
(
    [type] => VARCHAR
    [constraint] => 200
)

This is the final result I want:

$fields = array(
    'System_ID' => array(
            'type' => 'VARCHAR',
            'constraint' => '100',
    ),
);

Solution

If you want same value for all values from first array, then simple array_fill_keys will work:

$array1 = ['System ID', 'Last Name'];
$array2 = [
    'type' => 'VARCHAR',
    'constraint' => '200',
];
print_r(array_fill_keys($array1, $array2));

Demo.



Answered By - u_mulder

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.