首页 > 解决方案 > 如何从 SQL 查询中反序列化数据并在 JSON 中解析它们

问题描述

我有上面的 PHP 代码,以便从我的 MYSQL DB 中获取我的数据并将它们解析为 JSON。

$BusinessProductsData ='';
        $sql = "SELECT t1.meta_value, t1.post_id FROM `f2st_postmeta` AS t1 
                INNER JOIN
                (SELECT post_title AS post_title, ID FROM `f2st_posts` WHERE post_type='restaurants' and post_status='publish') AS t2
                ON t1.post_id=t2.ID
                WHERE t1.meta_key='foodbakery_menu_items'";

        mysqli_set_charset($dbConnection, "utf8");

        $result = mysqli_query($dbConnection, $sql);

        while ($row = mysqli_fetch_assoc($result)){
            $json_array[] = $row;
        }

        $BusinessProductsData = json_encode($json_array);
        echo $BusinessProductsData;

不幸的是, meta_value 已经序列化了像这样的值 {"meta_value":"a:2:{i:0;a:7:{s:15:\"menu_item_title\";s:89:\"Dr. Scholl Fungal Nail Treatment \u03a3\u03cd\u03c3\u03c4\u03b7\u03bc\u03b1 \u0391\u03bd\u03c4\u03b9\u03bc\u03b5\u03c4\u03ce\u03c0\u03b9\u03c3\u03b7\u03c2 \u039c\u03c5\u03ba\u03ae\u03c4\u03c9\u03bd\";s:15:\"restaurant_menu\";s:27:\"\u0395\u03af\u03b4\u03b7 \u03a6\u03c1\u03bf\u03bd\u03c4\u03af\u03b4\u03b1\u03c2\";s:21:\"menu_item_description\";s:0:\"\";s:14:\"menu_item_icon\";s:5:\"16665\";s:15:\"menu_item_nutri\";s:0:\"\";s:15:\"menu_item_price\";s:5:\"15.12\";s:15:\"menu_item_extra\";s:0:\"\";}i:1;a:7:{s:15:\"menu_item_title\";s:61:\"Optima Aloe Pura Aloe Vera Gel 200ml \u0396\u03b5\u03bb \u0391\u03bb\u03cc\u03b7 \u0392\u03ad\u03c1\u03b1\";s:15:\"restaurant_menu\";s:25:\"\u0395\u03af\u03b4\u03b7 \u039f\u03bc\u03bf\u03c1\u03c6\u03af\u03b1\u03c2\";s:21:\"menu_item_description\";s:0:\"\";s:14:\"menu_item_icon\";s:5:\"16666\";s:15:\"menu_item_nutri\";s:0:\"\";s:15:\"menu_item_price\";s:4:\"6.86\";s:15:\"menu_item_extra\";s:0:\"\";}}","post_id":"16220"},{"meta_value":"a:0:{}","post_id":"16331"}

如何反序列化 meta_value?

标签: mysqljsonwordpressserializationmetadata

解决方案


推荐阅读