我的数据库中有一个表,到目前为止,它非常适合在多个对象中存储内容。但我希望能够将其转换为多对象数组。
这是与这个新的'对象'(在mysql中)相关的数据:
uid field value page:1 shop[2].location In Shops, Dundas Arcades,Middlesbrough, TS1 1HT page:1 shop[1].location 5a High Street, Stockton-on-tees, TS18 1UB page:1 name Enter The Asylum page:1 contact.website http://entertheasylum.co.uk page:1 contact.phone 0800 090 090
现在我正在寻找的是通过PHP将其转换为类似(print_r输出)的东西:
array(
"name" => "Enter The Asylum",
"shop" => array(
array("location" => "In Shops, Dundas Arcades..."),
array("location" => "5a High Street, Stockton-on-tees...")
),
"contact" => array(
"website" => "http://entertheasylum.co.uk",
"phone" => "0800 090 090"
)
)
有人有任何想法吗?
答案 0 :(得分:1)
我无法帮助解决原始问题,但我建议使用像MySQL这样的关系数据库。也就是说,你的表应该有这样的结构:
表格页:
表page_location:
数据将看起来像这样。
表格页:
| id | name | phone | website |
| 1 | "Enter The Asylum" | "0800 090 090" | "http://entertheasylum.co.uk" |
表page_position:
| id | page_id | position | location |
| 1 | 1 | 1 | "5a High Street, Stockton-on-tees, TS18 1UB" |
| 2 | 1 | 2 | "In Shops, Dundas Arcades,Middlesbrough, TS1 1HT" |
还有一些额外的阅读:Relational model
答案 1 :(得分:0)
我已经通过正则表达式和php设法做到了这一点。这是神奇的功能(它是我的代码和东西的一部分):
function fetch_profile_info($id){
$sql = "SELECT * FROM profile_info WHERE `uid`='".me($id)."'";
$r = mysql_query($sql);
$profile = array();
while($row = mysql_fetch_array($r)){
/* new: process it into objects etc */
$field = $row['field'];
$field = preg_replace('/\.([a-zA-Z]*)/i', "['$1']", $field);
$field = preg_replace('/^([a-zA-Z]*)/', "['$1']", $field);
eval("\$profile{$field} = \"".addslashes($row['value'])."\";");
}
return $profile;
}
它有效!耶!