I have one array, and it has 10+ indexes.
What I want to do is set variable $table based on the index's, so that it will insert
Array[0] - Array[9] to $table = table1
and it would insert
Array[10] - Array[14] to $table = table2
I don't want to use an if statement because I need them both inserted
I was hoping to keep this all in one query and use $table (if it's possible)
How could I achieve this?
$table = array();
foreach($array as $key => $value)
if ($key <= 9)
$table['table1'][$key] = $value;
else
$table['table2'][$key] = $value;
This will keep it all as one array. Which I think is kinda what you were going for.
I'm terrible at SQL query so the bellow is just pseudo for if each key is a column in the table:
foreach($table as $key => $value){
if($key == 'table1'){
foreach($value as $key => $value){
//INSERT INTO table1 ($key) VALUES ($value)
{
if($key == 'table2'){
foreach($value as $key => $value){
//INSERT INTO table2 ($key) VALUES ($value)
{
}
So you create 2 arrays and work with them:
$table1 = array() ; //Save data into arrays so you can put it in a database (?)
$table2 = array() ;
foreach(array_values($array) as $key => $value){
if ($key <= 9)
$table1[] = $value ;
else
$table2[] = $value ;
}
Basically is splitting an array into sub arrays, right? If so, then look at array_chunk()