krishna krishna -4 years ago 110
PHP Question

How can I combine two arrays in into one single array?

I am having to add more functionality in my codeigniter project. Here is the jsfiddle demo. I have an array like this:

$faculty = array(2, 3, 4);


and

$message = array('test1', 'test2', 'test3');


I'm also having an last insert id from another database:

$last_inserted_id = 1;


I want to combine the data to form an array like this:

$array = array(array('faculty' => 2, 'message' => 'test1', 'id' => 1),
array('faculty' => 3, 'message' => 'test2', 'id' => 1),
array('faculty' => 4, 'message' => 'test3', 'id' => 1));


Thanks for everyone for your suggestions and time.

Answer Source

I hope this will you:

$array = [];
$faculty = array(2, 3, 4);
$message = array('test1', 'test2', 'test3');
$id = 1;
for ($key = 0; $key < count($faculty); $key++) {
    $array[$key]['faculty'] = $faculty[$key];
    $array[$key]['message'] = $message[$key];
    $array[$key]['id'] = $id;
}
print_r($array);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download