ludo1960 Posted January 21, 2019 Author Share Posted January 21, 2019 Hmmm, the only time i've seen odd characters floating about, was when I copied and pasted from this site. Here's the code again. i've triec mousepad and open office but nothing shows up, even tried https://marketplace.visualstudio.com/items?itemName=ShaneRay.InvisibleCharacterVisualizer#qna still no superfluous characters $db = pdoConnect(); $db->query("DROP TABLE IF EXISTS test2DB"); $db->query("CREATE TABLE test2DB ( ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY, Page_id INT, County VARCHAR(50) NOT NULL, Country VARCHAR(50) NOT NULL, Post_Town VARCHAR(50) NOT NULL )"); Quote Link to comment https://forums.phpfreaks.com/topic/308197-pdo-double-entries/page/2/#findComment-1563760 Share on other sites More sharing options...
Barand Posted January 21, 2019 Share Posted January 21, 2019 3 hours ago, ludo1960 said: array_push($values, array_values($myarray)); That line is wrong. It is pushing an array on to the $values array. You need to push the individual values from the array, hence the "..." in array_push($values, ...array_values($myarray)); Quote Link to comment https://forums.phpfreaks.com/topic/308197-pdo-double-entries/page/2/#findComment-1563761 Share on other sites More sharing options...
ludo1960 Posted January 21, 2019 Author Share Posted January 21, 2019 That works just fine, where can I find info describing the use of "..." never seen that in anybody elses code before? Quote Link to comment https://forums.phpfreaks.com/topic/308197-pdo-double-entries/page/2/#findComment-1563763 Share on other sites More sharing options...
Barand Posted January 21, 2019 Share Posted January 21, 2019 It's on this page of the manual. Scroll down a bit to "Variadic functions" Quote Link to comment https://forums.phpfreaks.com/topic/308197-pdo-double-entries/page/2/#findComment-1563767 Share on other sites More sharing options...
ludo1960 Posted January 22, 2019 Author Share Posted January 22, 2019 For anybody following on, here is the finished working code. If you're faced with a crazy multi dimensional array from a JSON source that you have to get into a mysql database, this is for you! <?php function FlattenMultiArray($array,$bKeepKeys=true,$key_prefix='') { $array_flattened=Array(); foreach($array as $key=>$value){ if(Is_Array($value)){ $array_flattened=Array_Merge( $array_flattened, FlattenMultiArray($value,$bKeepKeys,$key) ); } else{ if($bKeepKeys){ $array_flattened["{$key_prefix}{$key}"]=$value; } else{ $array_flattened[]=$value; } } } return $array_flattened; } include 'connection.php' ; $startnum = 41; $endnum = 45; //$placeholders = []; //not required! $values = []; for($i = $startnum; $i <= $endnum; ++$i) { $url = "http://api.somesite.com&page_number=$i"; $response = json_decode(file_get_contents($url), true); $resp = FlattenMultiArray($response, true, NULL); $myarray = array( 'Page_id' => $i, //your database table column name => data you want to insert 'County' => $resp['county'], 'Country' => $resp['country'], 'Post_Town' => $resp['0post_town'], ); $mykeys = implode(', ', array_keys($myarray)); $myplaceholders[] = '(' . implode (", ", array_fill(0, count($myarray), '?')) . ')'; array_push($values, ...array_values($myarray)); } //endfor $res = $db->prepare("INSERT INTO YourDB ($mykeys) VALUES " . join(', ', $myplaceholders)) ; $res->execute($values); Quote Link to comment https://forums.phpfreaks.com/topic/308197-pdo-double-entries/page/2/#findComment-1563774 Share on other sites More sharing options...
Barand Posted January 22, 2019 Share Posted January 22, 2019 The array flattening is just a waste of time and effort. Get the data from the multi-D array directly. It's not rocket science. $myarray = array( 'Page_id' => $i, //your database table column name => data you want to insert 'County' => $response['county'], 'Country' => $response['country'], 'Post_Town' => $response['listings'][0]['post_town'] ); Quote Link to comment https://forums.phpfreaks.com/topic/308197-pdo-double-entries/page/2/#findComment-1563776 Share on other sites More sharing options...
ludo1960 Posted January 22, 2019 Author Share Posted January 22, 2019 If you are a black belt at multi-D arrays the size of Donald Trumps' head, you're probably right, if not, use the flatten function. Quote Link to comment https://forums.phpfreaks.com/topic/308197-pdo-double-entries/page/2/#findComment-1563780 Share on other sites More sharing options...
Barand Posted January 22, 2019 Share Posted January 22, 2019 Below is the output from echo '<pre>', print_r($response, 1), '</pre>'; showing a cut-down version of your response array. All you need to do is step through the sequence of keys on the way down to the item you want. Quote Link to comment https://forums.phpfreaks.com/topic/308197-pdo-double-entries/page/2/#findComment-1563784 Share on other sites More sharing options...
ludo1960 Posted January 26, 2019 Author Share Posted January 26, 2019 The County and Country were the easy ones, do the $response['price'] ? Please! Quote Link to comment https://forums.phpfreaks.com/topic/308197-pdo-double-entries/page/2/#findComment-1563900 Share on other sites More sharing options...
Barand Posted January 26, 2019 Share Posted January 26, 2019 I don't have the full array I have shown you how to do it Quote Link to comment https://forums.phpfreaks.com/topic/308197-pdo-double-entries/page/2/#findComment-1563902 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.