如何使用PHP在Mysql中存储数组

时间:2021-07-25 05:55:22

I want to store array in mysql database. with key and value

我想在mysql数据库中存储数组。有关键和价值

Following are code i used

以下是我使用的代码

         $name= "prashant";
         $lname= "kumbhar";
         $test = "testing";

        $array = array('fname'=>$name,'lname'=>$lname,'test' =>$testing);

Store these $array in database using php

使用php将这些$数组存储在数据库中

3 个解决方案

#1


2  

Use json_encode() to store data in mysql table.

使用json_encode()将数据存储在mysql表中。

<?php
$name= "prashant";
$lname= "kumbhar";
$test = "testing";

$array = array('fname'=>$name,
               'lname'=>$lname,
               'test' => $test
         );

$res = json_encode($array);
echo "Convert from array to json :" .$res;

echo "\n\nFrom json to array:\n";
print_r(json_decode($res));

output

产量

Convert from array to json :
{"fname":"prashant","lname":"kumbhar","test":"testing"}

From json to array:
stdClass Object
(
    [fname] => prashant
    [lname] => kumbhar
    [test] => testing
)

At the time of retrieve data in array form then use json_decode()

在以数组形式检索数据时,然后使用json_decode()

Working Demo : Click Here

工作演示:点击这里

#2


4  

use convert array into json format using json_encode and then store in database, you can convert json string format back to array using json_decode. Example

使用json_encode将convert数组转换为json格式然后存储在数据库中,可以使用json_decode将json字符串格式转换回数组。例

  $name= "prashant";
  $lname= "kumbhar";
  $test = "testing";

  $array = array('fname'=>$name,'lname'=>$lname,'test' = $testing);
  $data_for_db = json_endcode($array);

You can convert stored json format data back to normal array as below

您可以将存储的json格式数据转换回普通数组,如下所示

$array = json_decode($data_for_db);

Thanks

谢谢

#3


3  

You can serialize the array and store the string in the database:

您可以序列化数组并将字符串存储在数据库中:

serialize ($array);

If you want to use it just unserialize the result that you've get from the database

如果要使用它,只需反序列化从数据库中获得的结果

$array = unserialize ($databaseResult);

If you prefer json you can do the same with json_encode and json_decode

如果你更喜欢json,你可以使用json_encode和json_decode

#1


2  

Use json_encode() to store data in mysql table.

使用json_encode()将数据存储在mysql表中。

<?php
$name= "prashant";
$lname= "kumbhar";
$test = "testing";

$array = array('fname'=>$name,
               'lname'=>$lname,
               'test' => $test
         );

$res = json_encode($array);
echo "Convert from array to json :" .$res;

echo "\n\nFrom json to array:\n";
print_r(json_decode($res));

output

产量

Convert from array to json :
{"fname":"prashant","lname":"kumbhar","test":"testing"}

From json to array:
stdClass Object
(
    [fname] => prashant
    [lname] => kumbhar
    [test] => testing
)

At the time of retrieve data in array form then use json_decode()

在以数组形式检索数据时,然后使用json_decode()

Working Demo : Click Here

工作演示:点击这里

#2


4  

use convert array into json format using json_encode and then store in database, you can convert json string format back to array using json_decode. Example

使用json_encode将convert数组转换为json格式然后存储在数据库中,可以使用json_decode将json字符串格式转换回数组。例

  $name= "prashant";
  $lname= "kumbhar";
  $test = "testing";

  $array = array('fname'=>$name,'lname'=>$lname,'test' = $testing);
  $data_for_db = json_endcode($array);

You can convert stored json format data back to normal array as below

您可以将存储的json格式数据转换回普通数组,如下所示

$array = json_decode($data_for_db);

Thanks

谢谢

#3


3  

You can serialize the array and store the string in the database:

您可以序列化数组并将字符串存储在数据库中:

serialize ($array);

If you want to use it just unserialize the result that you've get from the database

如果要使用它,只需反序列化从数据库中获得的结果

$array = unserialize ($databaseResult);

If you prefer json you can do the same with json_encode and json_decode

如果你更喜欢json,你可以使用json_encode和json_decode