I have tried both the links but I don't get the expected variables content in $fields and $newdata
** This question has developed in a new one here ** PHP how to extract keys names and values from associative array for mysql query (as explained I'm newbie and it took some work to get the question asked in the right way)
so I kindly print here some var_dump and cast to kindly ask your support.
For simplicity in my learning experiment, I'm working with a dummy table of just 5 fields, as you see they are: selected
, user_id
, user_name
, user_company
and user_email
.
Finally I have inserted just 2 rows of values.
Using this code
$Arr = (array)$data;
print_r ( $Arr );
I can see this output
Array (
[0] => Array ( [selected] => [user_id] => 3 [user_name] => nome3 [user_company] => azien3 [user_email] => email3 )
[1] => Array ( [selected] => 1 [user_id] => 6 [user_name] => nome6 [user_company] => azien6 [user_email] => email6 )
)
next I try to apply the code of your links
24 $fields = implode(",", array_keys($Arr));
25 $newdata = "'" . implode("','", $Arr) . "'";
26
27 var_dump($fields);
28 echo "<br><br>";
29 var_dump($newdata);
But something is wrong in my interpreteation or in my code , because the output is
Notice: Array to string conversion in D:\xampp\htdocs\ajax-json\post.php on line 25 Notice: Array to string conversion in D:\xampp\htdocs\ajax-json\post.php on line 25 string(3) "0,1" string(15) "'Array','Array'"
can you kindly point out what's wrong? e.g. is my array properly formed?
ORIGINAL QUESTION
I'm newbie and after further readings I got the point and hopefully now my question could result more simple. Thank you for any hint.
I have a bidimensional array since its records are coming from an html table.
The collection made on the html side generates an associative array so each row is like in the following example where, also, you can see, the keys are many
[item] => Array (
[0] => Array (
[unit] => "car"
[qty] => 3
[description] => "Description 1"
....
[key47] => "thin"
)
[1] => Array (
[unit] => "bike"
[qty] => 74
[description] => "Description 2"
....
[key47] => "large"
)
)
The mysql table name where I want to insert the items is named items_table
"unit" "qty" and "description" and all the remaining keys (total 47) are exactly the same names of the items_table columns
When on the php side I'd like to have a sort of automatic query generation maybe with PDO that would pick the columns names from the inner array keys names and will insert the corresponding key value in the correct column.
The goal is to avoid creating such a awkward and extremely long query.
Preferred method would be with PDO, avoid binding 47 keys.
This sort of automatism is also useful because in the future columns in the mysql table may being added and or removed and or renamed, so I'd like to forget about the query thinking only about properly editing the html table (on that side the array is already automatically generated with some javascript)
Do you think is possible to do this?
If yes how to?
Does this still need some foreach loop?
Thank you for helping