I am experimenting with datatables and php, and trying to get an example server side script working at https://www.datatables.net/examples/data_sources/server_side.html.
as part of this I need to create an array of arrays of the datatable columns that looks like:
$columns = array(
array( 'db' => 'first_name', 'dt' => 0 ),
array( 'db' => 'last_name', 'dt' => 1 ),
array( 'db' => 'position', 'dt' => 2 ),
array( 'db' => 'office', 'dt' => 3 ),
);
I have an array:
$mytablescolumns = array('name1','name2', ... )
which I would like to iterate through producing a 2d array that looks like:
$columns = array(
array( 'db' => 'name1', 'dt' => 0 ),
array( 'db' => 'name2', 'dt' => 1 ),
I've looked at http://php.net/manual/en/function.array-fill.php and http://php.net/manual/en/function.array-fill-keys.php , but I'm not sure if these will accomplish this since they appear to deal with 1D arrays. How can I accomplish this?
I think a simple foreach is the easiest:
$array = array();
foreach($mytablescolumns as $index => $column) {
$array[] = array('db' => $column, 'dt' => $index);
}
print_r($array);
This will create a new array in the $array
variable. If you want to modify the original array, you can use it this way:
foreach($mytablescolumns as $index => &$column) {
$column = array('db' => $column, 'dt' => $index);
}
print_r($mytablescolumns);