5

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?

1
  • Have you tried writing a simple foreach() loop to do so? Commented Jul 18, 2014 at 16:14

5 Answers 5

8

This is what I recently did to quickly create a two dimensional array (10x10), initialized to 0:

<?php
  $a = array_fill(0, 10, array_fill(0, 10, 0));
?>

This should work for as many dimensions as you want, each time passing to array_fill() (as the 3rd argument) another array_fill() function.

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

1 Comment

I don't see how this possibly resolves the asked question.
7

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);

Comments

3

How about that one?

for($i = 0; $i < count($mytablescolumns); $i++) {
   $columns[$i] = array('db'=>$mytablescolumns[$i],'dt'=>$i);
}

Comments

0

This's a mixing solution between "for/foreach" loop and "array_fill" that I like to do for a fixed value:

for ($i = 0; $i < 10; $i++) {
    $array[] = array_fill(0, 10, -1);
}

The above code creates a two dimensional array 10x10 initialized to -1

1 Comment

I don't see how this possibly resolves the asked question.
0

For completeness, here are some other approaches to iterate the input array and output the desired 2d array.

Use compact() in each iteration for brevity. (Demo)

$result = [];
foreach ($mytablescolumns as $dt => $db) {
    $result[] = compact('db', 'dt');
}
var_export($result);

Use array_map() to iterate and get_defined_vars() for brevity. (Demo)

var_export(
    array_map(
        fn($db, $dt) => get_defined_vars(),
        $mytablescolumns,
        array_keys($mytablescolumns)
    )
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.