【发布时间】:2017-10-10 21:50:38
【问题描述】:
我正在从 Wordpress 数据库生成 JSON,到目前为止,我需要在 PHP 中排序以下 JSON 块。它很乱,但数据库是由插件以这种方式设置的。
这将是一个食物菜单(目前是虚拟数据),每个项目都有一个类别,如主菜、甜点。每个数字meta_key 增量是客户添加的菜单上的不同项目,并且标题、描述、图像和价格以这种方式连接。
数据按帖子 ID 分组,然后我需要将所有主菜分组在一起,然后是甜点等。数据有点混乱,因为每个菜单项都有一个递增的数字 meta_key,这是由最终用户基于他们添加了多少项目。
JSON如下,如果你能帮忙,我会写我需要实现的。
[
{
"post_id": 2202,
"meta_key": "_menu_cat_0",
"meta_value": "Mains"
},
{
"post_id": 2202,
"meta_key": "_menu_cat_1",
"meta_value": "Mains"
},
{
"post_id": 2202,
"meta_key": "_menu_cat_2",
"meta_value": "Mains"
},
{
"post_id": 2202,
"meta_key": "_menu_cat_3",
"meta_value": "Desserts"
},
{ "post_id": 2202,
"meta_key": "_menu_d_image_id_0",
"meta_value": "2246"
},
{
"post_id": 2202,
"meta_key": "_menu_d_price_0",
"meta_value": "12233"
},
{
"post_id": 2202,
"meta_key": "_menu_d_title_0",
"meta_value": "dia title boxee"
},
{
"post_id": 2202,
"meta_key": "_menu_description_0",
"meta_value": "With a choice of either; Mature cheddar, American cheese, Red Leicester or Applewood smoked cheddar. Served with our original HBC burger relish, mayo, lettuce, tomato & red onion"
},
{
"post_id": 2202,
"meta_key": "_menu_description_1",
"meta_value": "Other"
},
{
"post_id": 2202,
"meta_key": "_menu_description_2",
"meta_value": "With a choice of either; Mature cheddar, American cheese, Red Leicester or Applewood smoked cheddar. Served with our original HBC burger relish, mayo, lettuce, tomato & red onion"
},
{
"post_id": 2202,
"meta_key": "_menu_description_3",
"meta_value": "With a choice of either; Mature cheddar, American cheese, Red Leicester or Applewood smoked cheddar. Served with our original HBC burger relish, mayo, lettuce, tomato & red onion"
},
{
"post_id": 2202,
"meta_key": "_menu_image_id_0",
"meta_value": "2256"
},
{
"post_id": 2202,
"meta_key": "_menu_image_id_1",
"meta_value": "2269"
},
{
"post_id": 2202,
"meta_key": "_menu_image_id_2",
"meta_value": "2256"
},
{
"post_id": 2202,
"meta_key": "_menu_image_id_3",
"meta_value": "2256"
},
{
"post_id": 2202,
"meta_key": "_menu_order_0",
"meta_value": ""
},
{
"post_id": 2202,
"meta_key": "_menu_order_1",
"meta_value": ""
},
{
"post_id": 2202,
"meta_key": "_menu_order_2",
"meta_value": ""
},
{
"post_id": 2202,
"meta_key": "_menu_order_3",
"meta_value": ""
},
{
"post_id": 2202,
"meta_key": "_menu_price_0",
"meta_value": "12.99"
},
{
"post_id": 2202,
"meta_key": "_menu_price_1",
"meta_value": "14.99"
},
{
"post_id": 2202,
"meta_key": "_menu_price_2",
"meta_value": "1.99"
},
{
"post_id": 2202,
"meta_key": "_menu_price_3",
"meta_value": "3.85"
},
{
"post_id": 2202,
"meta_key": "_menu_title_0",
"meta_value": "Mains 1"
},
{
"post_id": 2202,
"meta_key": "_menu_title_1",
"meta_value": "Mains 2"
},
{
"post_id": 2202,
"meta_key": "_menu_title_2",
"meta_value": "Mains 3"
},
{
"post_id": 2202,
"meta_key": "_menu_title_3",
"meta_value": "Dessert 1"
}
]
我想要达到的目标:
[
"Mains" {
{
"post_id": 2202,
"_menu_title_0": "Food title",
"_menu_description_0": "food desc",
"_menu_price_0": "food price",
"_menu_image_id_0": "url"
},
{
"post_id": 2202,
"_menu_title_1": "Food title",
"_menu_description_1": "food desc",
"_menu_price_1": "food price",
"_menu_image_id_1": "url"
},
{
"post_id": 2202,
"_menu_title_2": "Food title",
"_menu_description_2": "food desc",
"_menu_price_2": "food price",
"_menu_image_id_2": "url"
},
}
"Desserts" {
{
"post_id": 2202,
"_menu_title_0": "dessert title",
"_menu_description_0": "des food desc",
"_menu_price_0": "des food price",
"_menu_image_id_0": "url"
},
{
"post_id": 2202,
"_menu_title_1": "Food title",
"_menu_description_1": "food desc",
"_menu_price_1": "food price",
"_menu_image_id_1": "url"
},
{
"post_id": 2202,
"_menu_title_2": "Food title",
"_menu_description_2": "food desc",
"_menu_price_2": "food price",
"_menu_image_id_2": "url"
},
}
]
以此类推,每次在 PHP 中循环时都会使用新的类别数组进行更改。生成现有 JSON 的 PHP 代码如下:
$list = array();
$info5 = $q->doQuery("SELECT post_id, meta_key, meta_value from wp_postmeta WHERE post_id = ? AND meta_key LIKE '_menu_%'", array($postID));
foreach($info5 as $row){
array_push($list,$row);
}
asort($list);
foreach ($list as $key => $val) {
array_push($json,$key = $val);
}
echo json_encode($json, JSON_PRETTY_PRINT);
提前致谢。
【问题讨论】:
-
您的原始 json 条目缺少对大多数条目的主菜或甜点的引用。您如何知道哪些与哪个类别相匹配?
-
我试图以数字方式匹配元键并将它们分组到 _menu_cat_$i 的类别下。
-
你可以使用 WP_Query() 来获取自定义帖子类型“菜单项”的所有信息,然后使用 get_post_meta() 来获取所有额外信息。这样,您可以在 PHP 中构建数组数组,以完成的格式作为 json 传递。我使用 POD 构建简单的自定义帖子类型。你也可以在functions.php文件codex.wordpress.org/Function_Reference/register_post_type
标签: php arrays json sorting multidimensional-array