Home > Backend Development > PHP Problem > How to access mysql array in php

How to access mysql array in php

angryTom
Release: 2023-02-28 09:28:02
Original
2440 people have browsed it

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" => "产品经理"), 
);
Copy after login

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解码成数组
Copy after login

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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template