Hi, I’m trying to create a method that outputs an array in this format:
Array
(
[0] => Array
(
[name] => home
[id] => 1
)
[1] => Array
(
[id] => 2
[name] => about
)
)
The problem is I want to generate the number of keys in the array dynamically. So instead of just writing array(‘id’ => $idValue, ‘name’ => $nameValue); the number of keys has to be flexible, since this method is used to generate a table with x number of columns.
So far I have:
while ($query->fetch()) {
for ($i = 0; $i < count($columnNames); $i++ ) {
$pagelisting[] = array($columnNames[$i] => db_connection::get_column($columnNames[$i]));
}
}
that generates this:
Array
(
[0] => Array
(
[name] => home
)
[1] => Array
(
[id] => 1
)
[2] => Array
(
[name] => About
)
[3] => Array
(
[id] => 2
)
)
I don’t want the name and id to be separated into a new array each time. I tried using array_splice but it didn’t quite get the right result, or maybe it can?