我已经通过Gravity Forms将一些数据添加到Wordpress站点,我试图以人类可读的格式循环存储在数据库中的数据。存储的数据用于MOVIE,ROLE和YEAR - 以下是数据存储在数据库中的方式:
S:298: “A:3:{I:0;一个:3:{S:4:” 年 “; S:4:” 2010 “; S:5:” 电影“; S:18: “不可能的任务”; s:4:“角色”; s:5:“演员”;} i:1; a:3:{s:4:“年”; s:4:“2011”; s:5 :“电影”; s:20:“碟中谍2”; s:4:“角色”; s:10:“主演”;} i:2; a:3:{s:4:“年”; s:4:“2012”; s:5:“电影”; s:20:“不可能的任务3”; s:4:“角色”; s:8:“导演”;}}“;
任何有关如何循环并以人类可读的方式输出数据的想法都将非常受欢迎......我会向任何可以提供帮助的人发送啤酒......
提前致谢!
答案 0 :(得分:1)
你试过unserialize这个字符串吗?
答案 1 :(得分:1)
$data = 's:298:"a:3:{i:0;a:3:{s:4:"Year";s:4:"2010";s:5:"Movie";s:18:"Mission Impossible";s:4:"Role";s:5:"Actor";}i:1;a:3:{s:4:"Year";s:4:"2011";s:5:"Movie";s:20:"Mission Impossible 2";s:4:"Role";s:10:"Lead Actor";}i:2;a:3:{s:4:"Year";s:4:"2012";s:5:"Movie";s:20:"Mission Impossible 3";s:4:"Role";s:8:"Director";}}";';
// first remove the string declaration at the start, if you don't do this, unserialize() will treat most of the data as a string and won't convert it to an array
$data = substr($data, strpos($data, '"')+1);
// now just unserialize into an array so it can be looped through with foreach() etc
$arr = unserialize($data);
print_r($arr);
输出
Array
(
[0] => Array
(
[Year] => 2010
[Movie] => Mission Impossible
[Role] => Actor
)
[1] => Array
(
[Year] => 2011
[Movie] => Mission Impossible 2
[Role] => Lead Actor
)
[2] => Array
(
[Year] => 2012
[Movie] => Mission Impossible 3
[Role] => Director
)
)
要循环播放数组,您可以使用foreach
:
<?php
$html = '<table>';
// table headings
$html .= '<tr><th>Year</th><th>Movie</th><th>Role</th></tr>';
foreach($arr as $item)
{
$html .= '<tr>';
$html .= '<td>' . $item['Year'] . '</td>';
$html .= '<td>' . $item['Movie'] . '</td>';
$html .= '<td>' . $item['Role'] . '</td>';
$html .= '</tr>';
}
$html .= '</table>';
echo $html; // output the HTML table
?>