php - 如何从wordpress数据库的元值中获取数据

标签 php mysql json wordpress

我是一名 PHP 开发新手

这是我想要在页面上显示的meta_value:

a: 5: {
        i: 0;a: 8: {
            s: 4: "mode";s: 7: "builder";s: 4: "name";s: 26: "Service Dog Handler Name *";s: 5: "value";s: 12: "handler name";s: 5: "price";s: 1: "0";s: 7: "section";s: 23: "555453917016c5.41306136";s: 13: "section_label";s: 26: "Service Dog Handler Name *";s: 19: "percentcurrenttotal";i: 0;s: 8: "quantity";i: 1;
        }
        i: 1;a: 8: {
            s: 4: "mode";s: 7: "builder";s: 4: "name";s: 18: "Service Dog Name *";s: 5: "value";s: 8: "dog name";s: 5: "price";s: 1: "0";s: 7: "section";s: 23: "55545e36c8cbd4.93742171";s: 13: "section_label";s: 18: "Service Dog Name *";s: 19: "percentcurrenttotal";i: 0;s: 8: "quantity";i: 1;
        }
        i: 2;a: 12: {
            s: 4: "mode";s: 7: "builder";s: 8: "multiple";s: 1: "1";s: 3: "key";s: 9: "1 ($14)_1";s: 4: "name";s: 27: "Add Extra ID Cards to Order";s: 5: "value";s: 7: "1 ($14)";s: 5: "price";s: 2: "14";s: 7: "section";s: 23: "555453a17016d1.12489612";s: 13: "section_label";s: 27: "Add Extra ID Cards to Order";s: 19: "percentcurrenttotal";i: 0;s: 8: "quantity";i: 1;s: 10: "use_images";s: 0: "";s: 6: "images";s: 0: "";
        }
        i: 3;a: 12: {
            s: 4: "mode";s: 7: "builder";s: 8: "multiple";s: 1: "1";s: 3: "key";s: 9: "1 ($16)_1";s: 4: "name";s: 35: "Add Extra Service Dog Tags to Order";s: 5: "value";s: 7: "1 ($16)";s: 5: "price";s: 2: "16";s: 7: "section";s: 23: "55545e71c8cbe5.09933250";s: 13: "section_label";s: 35: "Add Extra Service Dog Tags to Order";s: 19: "percentcurrenttotal";i: 0;s: 8: "quantity";i: 1;s: 10: "use_images";s: 0: "";s: 6: "images";s: 0: "";
        }
        i: 4;a: 8: {
            s: 4: "name";s: 15: "Add Dog Photo *";s: 5: "value";s: 152:"http://sitedemo.evirtualservices.com/registermyserviceanimal.com/site/wp-content/uploads/extra_product_options/c4ca4238a0b923820dcc509a6f75849b/dog1.jpg";s: 7: "display";s: 197: "<a href="
                http: //sitedemo.evirtualservices.com/registermyserviceanimal.com/site/wp-content/uploads/extra_product_options/c4ca4238a0b923820dcc509a6f75849b/dog1.jpg">dog1.jpg</a>";s:5:"price";s:1:"0";s:7:"section";s:23:"555453a67016e7.57135684";s:13:"section_label";s:15:"Add Dog Photo *";s:19:"percentcurrenttotal";i:0;s:8:"quantity";i:1;}}

我从 wordpress 的数据库中获取这个值。

如何在 php 中获取这些值?

最佳答案

您需要反序列化meta_value,它将将该值输出为嵌套数组。

$value is the meta_value you are getting from executing the database query.

$asdf = unserialize ($value);
echo "<pre>"; print_r($asdf);

它将以嵌套数组格式打印输出 -

Array
(
[0] => Array
    (
        [mode] => builder
        [name] => Service Dog Handler Name *
        [value] => handler name
        [price] => 0
        [section] => 555453917016c5.41306136
        [section_label] => Service Dog Handler Name *
        [percentcurrenttotal] => 0
        [quantity] => 1
    )

[1] => Array
    (
        [mode] => builder
        [name] => Service Dog Name *
        [value] => dog name
        [price] => 0
        [section] => 55545e36c8cbd4.93742171
        [section_label] => Service Dog Name *
        [percentcurrenttotal] => 0
        [quantity] => 1
    )
<小时/>

之后,您可以使用数组索引获取值 -

echo $asdf[0]['name']; // it will print the name

或使用嵌套数组格式

foreach($asdf as $qwer) 
   { 
     if (is_array($qwer)){
    foreach ($qwer as $value) {
        echo "<pre>"; print_r($value);
    }
}
}

它对我有用,希望有帮助。

关于php - 如何从wordpress数据库的元值中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30304806/

相关文章:

mysql给出不推荐使用的警告

mysql - Rails update_all 在字段列表中返回未知列 *

php - yii2 在后端拒绝用户登录

启用 session 时,PHP mysql_connect 无法解析主机名

php - 在 CakePHP 中上传文件的最佳实践

javascript - 当尝试将上传文件的路径插入 MySQL 时,会创建重复文件

php - Json 与 jquery

php - 我如何在 php 中的简单数组中包含关联数组?

javascript - JS : merging 2 arrays within an object

javascript - AngularJS json多维数组看起来不像它应该的那样