简体   繁体   中英

How to store MySql values as Multidimensional Array using PHP

I have a database table as follows.

<table border='1'><th>Id</th><th>FirstName</th><th>last Name</th><tr><td>1</td><td>Tom</td><td>T</td></tr><tr><td>2</td><td>Jerry</td><td>J</td></tr></table>

I would like to store all values as a multi dimensional array using php(using a while loop to retrieve fields).That is, I would like the data to be echoed as:

array(array(1,Tom,T),array(2,Jerry,J));
$result = mysql_query("SELECT * FROM tablename;");
while($result_ar = mysql_fetch_array($result)) {
    $multid_array[] = $result_ar;
}

after which $multid_array will be an array of arrays.

You can use phps serialize function to convert any variable into a string representation

$string = serialize($dbData);

You can use unserialize() to convert the string back into arrays, objects, etc

$dbData = unserialize($string);

Once you have the data within a string, it's very easy to store in a file, db, etc. A disadvantage is that you won't be able to search your database easily for this data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM