PHP数组 PHP保存数组到数据库

PHP数组 PHP保存数组到数据库

PHP保存数组到数据库

数组是 PHP 开发中使用最多的数据类型之一,对于结构化的数据尤为重要。

很多时候我们需要把数组保存到数据库中,实现对结构化数据的直接存储和读取。

其中一个案例就是,对于 Form 提交的多选 checkbox 数据,PHP后端就收到的数据就是一个数组,而且有可能还是多维数组。例如,如下一个员工数组:

$staff = array
(
 array("name" => "洪七", "number" => "101", "sex" => "男", "job" => "总经理", "mobile" => array("01234567890", "9876543210")), 
 array("name" => "郭靖", "number" => "102", "sex" => "男", "job" => "开发工程师"), 
 array("name" => "黄蓉", "number" => "103", "sex" => "女", "job" => "产品经理"), 
);

对于这样的数据,MySQL数据库是无法直接写入的,那么有什么办法呢?

有,但是需要稍微转换一下,不过也很简单。就是使用PHP自带的serialize()或者json_encode()函数序列化数据成字符串:

// 写入数据库之前
$staff_serialize = serialize($staff);            // 序列化成字符串
$staff_json = json_encode($staff);               // JSON编码数组成字符串

// 读取数据库后
$staff_restore = unserialize($staff_serialize);  // 反序列化成数组
$staff_dejson = json_decode($staff_json, true);  // JSON解码成数组

之后从数据库里面读出来的数据还是字符串格式的,用unserialize()json_decode()函数转换成数组就可以了。

 

<?php 
            $po = 'good';
                $arr=array();
                //$cars=array("Volvo","BMW","Toyota");
                //array[‘key’]=value
                $arr[]='1';
                $arr[]='2';
                $arr[]='3';
                $arr[]=$po;
                echo "I like " . $arr[0] . “, ” . $arr[1] . ” and ” . $arr[2] .  ” and ” . $arr[3].".";
                $arrlength=count($arr);
 
                for($x=0;$x<$arrlength;$x++)
                {
                    echo $arr[$x];
                    echo $x;
                    echo "<br>";
                }
                
            
$place=array("Peter"=>"352","Ben"=>"317","Joe6"=>"43");
$place[‘Arkansas’] = '23'; 
$staff_json = json_encode($place);               // JSON编码数组成字符串
$staff_dejson = json_decode($staff_json, true);  // JSON解码成数组
 
foreach($staff_dejson as $x=>$x_value)
{
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}

 echo  $staff_json;
 
 

foreach($staff_dejson as $x=>$x_value)
{
   echo '<tr>
        <td></td>
        <td><span class="radio"><label>';
        echo '  <input type="radio" name="pid" id="optionsRadios1" value="'.$x_value.'" checked="">'.$x.'</label></span>';
    echo '    </td>
    </tr>';
}
 
?>

修改时间

2017/10/25 09:28

Team Members:
friend

Comments (258)

Generic placeholder image
Jeremy Tomlinson
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
Generic placeholder image
Kathleen Thomas
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
Progress
Settings

Basic Settings


Recent Activity

You sold an item Paul Burgess just purchased “Hyper - Admin Dashboard”!

5 minutes ago

Product on the Bootstrap Market Dave Gamache added Admin Dashboard

30 minutes ago

Robert Delaney Send you message "Are you there?"

2 hours ago

Audrey Tobey Uploaded a photo "Error.jpg"

14 hours ago

You sold an item Paul Burgess just purchased “Hyper - Admin Dashboard”!

1 day ago