I have a huge json array. Some elements of the json array are serialized json arrays themselves. The problem is I have a large series of table inserts to do using this large json array. I am also using pdo
.
How can i quickly turn this json array, which has serialized json arrays themselves, into table inserts using pdo
?
It just seems like its taking too much time, and it doesn't seem like a clean way of doing things.
Basically, I have an interface that inserts into tables using the json array, and I need to convert it into table inserts using columns and placeholders with pdo. There are just too much elements in the array to insert.
I tried to automate the creation of inserts using this code and it works fine, and I like the way it came out, but I can't use this code or automate it with the json arrays whose elements have serialized json arrays themselves.
function create_query( $table_name, $cols, $placeholders )
{
$query= "INSERT INTO $table_name (
$cols
)
VALUES (
$placeholders
)";
}
/*
arg1= table_name;
*/
function insert_into_table( $argums, $location )
{
$num_classes= $location[ "num_classes" ];
foreach( $location as $key => $value )
{
$$key= $value;
}
$keys= array_keys( $argums );
$table_name = $keys[ 0 ];
array_shift( $keys );
array_shift( $argums );
$cols= implode( ", ", $keys );
$bound_array= array();
echo var_dump( $keys );
foreach( $keys as &$value )
{
$bound_array[ $value ]= ${ $argums[ $value ] };
$value= ":".$value;
}
$placeholders= implode( ", ", $keys );
$query= create_query( $table_name, $cols, $placeholders );
return array( "query" => $query, "bind" => $bound_array );
}
function do_insert( $arr )
{
global $db;
echo var_dump( $arr );
$query= $arr[ "query" ];
$bind= $arr[ "bind" ];
$stmt= $db->prepare( $query );
echo var_dump( $stmt );
if( !$stmt->execute( $bind ) )
{
echo var_dump( $stmt->errorInfo() );
}
$affected_rows= $stmt->rowCount();
}
$assoc_arr= insert_into_table( array( "company" => "company", 'companyName'=> "companyName", 'pkid' => 'pkid', 'reminderEmailTop' => 'reminderEmailTop', 'reminderEmailBottom' => 'reminderEmailBottom', 'reminderLink' => 'reminderLink' ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "basiclife_type", "page_nav" => "basiclife_pagenav", "orderby" => "basiclife_orderby" ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "bene_type", "page_nav" => "bene_pagenav", "orderby" => "bene_orderby" ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "beneper_type", "page_nav" => "beneper_pagenav", "orderby" => "beneper_orderby" ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "dent_type", "page_nav" => "dent_pagenav", "orderby" => "dent_orderby" ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "eflex_type", "page_nav" => "eflex_pagenav", "orderby" => "eflex_orderby" ), $location );
do_insert( $assoc_arr );
$assoc_arr= insert_into_table( array( "companyNav" => "companyNav", "companyId" => "pkid", 'type'=> "hsa_type", "page_nav" => "hsa_pagenav", "orderby" => "hsa_orderby" ), $location );
do_insert( $assoc_arr );
....and about 50 more of these type of inserts..
however with the inserts that have elements who are json arrays themselves i couldn't use the above code.
so i created a function which is just one manually typed insert...