Home > Article > Backend Development > How to access mysql array in php
How does php access mysql arrays
Arrays are one of the most commonly used data types in PHP development. Structured data is particularly important.
Many times we need to save arrays to the database to achieve direct storage and reading of structured data.
One of the cases is that for the multi-select checkbox data submitted by Form, the data received by the PHP backend is an array, and it may be a multi-dimensional array. For example, the following employee array:
$staff = array( array("name" => "张三", "number" => "101", "sex" => "男", "job" => "总经理", "mobile" => array("01234567890", "9876543210")), array("name" => "王五", "number" => "102", "sex" => "男", "job" => "开发工程师"), array("name" => "李六", "number" => "103", "sex" => "女", "job" => "产品经理"), );
For such data, the MySQL database cannot be written directly. We need to convert it slightly and use PHP's own serialize() or json_encode() function to serialize the data. Into a string:
// 写入数据库之前 $staff_serialize = serialize($staff);// 序列化成字符串 $staff_json = json_encode($staff); // JSON编码数组成字符串 // 读取数据库后 $staff_restore = unserialize($staff_serialize); // 反序列化成数组 $staff_dejson = json_decode($staff_json, true); // JSON解码成数组
The data read from the database using PHP is still in string format. Just use the unserialize() and json_decode() functions to convert it into an array.
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of How to access mysql array in php. For more information, please follow other related articles on the PHP Chinese website!