我如何将这个数组值转换为字符串以使我能够将其插入数据库。
$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);
答案 0 :(得分:1)
方法1:
您可以通过将其转换为sting
来插入完整数组。因此,您可以在插入之前使用PHP serialize
函数。
$serialized_data = serialize($myarray);
然后将用户$serialized_data
插入您的插入查询中。
参考:https://www.php.net/manual/en/function.serialize.php
注意,您将在使用此数据时取消序列化。因此,从数据库中检索后,可以通过unserialize()
函数对其进行反序列化。
另一种方式:您可以使用json_encode()
函数来存储array
。并且您还需要在何时/何地使用此数据使用json_decode()
。
答案 1 :(得分:1)
将表列定义为longText 像这样
$table->longText('column_name');
然后将数据另存为JSON STRING
$value = json_encode($myArray);