Php 如何将此数组插入数据库

Php 如何将此数组插入数据库,php,mysql,arrays,Php,Mysql,Arrays,如何将此数组值转换为字符串,以便将其插入数据库 $myArray = Array ( [0] => Array ( [code] => 1 [name] => Array ( [content] => Ohtels Villa Dorada ) [description]

如何将此数组值转换为字符串,以便将其插入数据库

$myArray = Array
(
    [0] => Array
        (
            [code] => 1
            [name] => Array
                (
                    [content] => Ohtels Villa Dorada
                )

            [description] => Array
                (
                    [content] => This hotel is located about 150 metres from the fine sandy beach. The lively centre of Cambrils is approximately 10 km away and can be easily reached by the public bus services. There is a stop for public transport right in front of the hotel. The immediate vicinity offers a diverse range of shopping and entertainment facilities including boutiques, restaurants and bars. This hotel comprises a total of 260 rooms spread over 5 floors. Dining options include a café, a bar and an air-conditioned buffet restaurant with highchairs for infants. The tastefully decorated, cosy rooms come with a balcony and satellite TV.
                )

            [countryCode] => ES
            [stateCode] => 43
            [destinationCode] => SAL
            [zoneCode] => 10
            [coordinates] => Array
                (
                    [longitude] => 1.152529
                    [latitude] => 41.068407
                )

            [categoryCode] => 3EST
            [categoryGroupCode] => GRUPO3
            [chainCode] => OHTEL
            [accommodationTypeCode] => HOTEL
            [boardCodes] => Array
                (
                    [0] => BB
                    [1] => AI
                    [2] => HB
                    [3] => FB
                    [4] => RO
                )

            [segmentCodes] => Array
                (
                    [0] => 37
                )

            [address] => Array
                (
                    [content] => Carrer Del Vendrell,11  
                )

            [postalCode] => 43840
            [city] => Array
                (
                    [content] => SALOU
                )

            [email] => comercial@ohtels.es
            [license] => HT-000473
            [web] => http://www.ohtels.es/
            [lastUpdate] => 2019-03-14
            [S2C] => 4*
            [ranking] => 96
        )
    [1] => Array
        (
            [code] => 1
            [name] => Array
                (
                    [content] => Sample
                )

            [description] => Array
                (
                    [content] => This hotel is located about 150 metres from the fine sandy beach. The lively centre of Cambrils is approximately 10 km away and can be easily reached by the public bus services. There is a stop for public transport right in front of the hotel. The immediate vicinity offers a diverse range of shopping and entertainment facilities including boutiques, restaurants and bars. This hotel comprises a total of 260 rooms spread over 5 floors. Dining options include a café, a bar and an air-conditioned buffet restaurant with highchairs for infants. The tastefully decorated, cosy rooms come with a balcony and satellite TV.
                )

            [countryCode] => ES
            [stateCode] => 43
            [destinationCode] => SAL
            [zoneCode] => 10
            [coordinates] => Array
                (
                    [longitude] => 1.152529
                    [latitude] => 41.068407
                )

            [categoryCode] => 3EST
            [categoryGroupCode] => GRUPO3
            [chainCode] => OHTEL
            [accommodationTypeCode] => HOTEL
            [boardCodes] => Array
                (
                    [0] => BB
                    [1] => AI
                    [2] => HB
                    [3] => FB
                    [4] => RO
                )

            [segmentCodes] => Array
                (
                    [0] => 37
                )

            [address] => Array
                (
                    [content] => Carrer Del Vendrell,11  
                )

            [postalCode] => 43840
            [city] => Array
                (
                    [content] => SALOU
                )

            [email] => comercial@ohtels.es
            [license] => HT-000473
            [web] => http://www.ohtels.es/
            [lastUpdate] => 2019-03-14
            [S2C] => 4*
            [ranking] => 96
        )
)
我希望将
$myArray
的所有值插入数据库

但我也使用了
foreach
循环将其存储到
$variable
中,我将把该值关联到查询中

INSERT INTO test(code,contents) VALUES $variable;
这就是我正在做的

$hotel_content = '';
foreach($myArray as $content)
{
  $hotel_content  .= "(".$content['code'].",".json_encode($content)."),";
}

$hotel = "INSERT INTO test(code,contents) VALUES ".rtrim($hotel_content,",").';';

if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
}

if (mysqli_query($conn, $hotel) ) 
{
    echo "You have successfully inserted the data.";
} 
else 
{
  echo "Error: " . $hotel . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
$hotel\u content='';
foreach($myArray作为$content)
{
$hotel_content.=“(“$content['code']”,“.json_encode($content)。”)”;
}
$hotel=“插入测试(代码、内容)值”。rtrim($hotel_content,“,”);
如果(!$conn){
die(“连接失败:”.mysqli_connect_error());
}
if(mysqli_查询($conn,$hotel))
{
echo“您已成功插入数据。”;
} 
其他的
{
echo“Error:.$hotel.”
“.mysqli_Error($conn); } mysqli_close($conn);
方式1: 您可以通过将其转换为
sting
插入完整数组。因此,您可以在插入之前使用PHP
序列化
函数

$serialized_data = serialize($myarray);
然后将用户
$serialized_data
插入到插入查询中。 参考:

注意使用此数据时必须取消序列化。因此,从数据库中检索后,通过
unserialize()
函数取消序列化它

另一种方法:您可以使用
json\u encode()
函数来存储
数组
。您还需要在何时何地使用此数据时使用
json\u decode()


参考:

将表列定义为长文本 像这样

 $table->longText('column_name');
然后将数据保存为JSON字符串

$value = json_encode($myArray);

你的桌子结构是什么?我怀疑您是否希望将整个数组序列化存储到数据库中,这与使用RDBMS
json\u encode将其转换为字符串的目的背道而驰。根据mySQL的版本,您甚至可能有一个可用的
JSON
列类型-无论哪种方式,它都是一个字符串,并且可以存储。当我使用JSON_encode()时,结果是用大括号括起来的。这就是为什么如果我将它与值关联,我会收到一个关于大括号的错误,这与serialize相同。我认为mysql不接受大括号?@MiksAlibo您的查询必须参数化,否则肯定会导致mysqlerror@MiksAlibo阅读将是一个很好的开端,我也会这样做。但我认为mysql不接受字符串中的大括号。