我正试图在mysql中插入一个关联数组,数据已插入,但插入了两次。
这是我的数组$newarr
,
array ([apple]=> red,[orange] => orange,[banana] => yellow)
这是我的代码:
<?php
include 'connection.php' ;
$url = "http://admin.contenta.com/api";
$jsondata = file_get_contents($url);
$response = json_decode($jsondata, true);
$myarray = $response['links'] ;
foreach ($myarray as $k => $v) {
$newarr[$k] = $v['href'];
}
$res = $db->prepare("INSERT INTO apidata (item, value) VALUES (?,?)") ;
foreach ($newarr as $item => $val) {
$res->execute( [ $item, $val ] );
}
echo "<pre>"; print_r( $newarr, 0 ); echo "</pre>"; // here are the 3
itemss returned