从 MySQL 序列化数组中检索特定值

发布于 2024-12-07 05:59:57 字数 735 浏览 0 评论 0原文

数据库结构如下:

id|metaKey|metaValue
--+-------+-----------------------------------
55|product|a:8:{s:3:"sku";s:0:"";s:8:"products";a:3:{i:1;a:3:{s:6:"option";s:1:"1";s:5:"price";s:5:"14.95";s:9:"saleprice";s:0:"";}i:2;a:3:{s:6:"option";s:0:"";s:5:"price";s:0:"";s:9:"saleprice";s:0:"";}i:3;a:3:{s:6:"option";s:0:"";s:5:"price";s:0:"";s:9:"saleprice";s:0:"";}}s:11:"description";s:124:"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";s:8:"shiprate";s:1:"A";s:8:"featured";s:2:"no";s:4:"sale";s:3:"yes";s:10:"cart_radio";s:1:"0";s:6:"optset";s:0:"";}

这是来自 wordpress 站点,我需要检索第一个“价格”值,即 14.95 从存储在 mysql 中的序列化数组中检索特定值的最佳方法是什么?

Database structure is as follows:

id|metaKey|metaValue
--+-------+-----------------------------------
55|product|a:8:{s:3:"sku";s:0:"";s:8:"products";a:3:{i:1;a:3:{s:6:"option";s:1:"1";s:5:"price";s:5:"14.95";s:9:"saleprice";s:0:"";}i:2;a:3:{s:6:"option";s:0:"";s:5:"price";s:0:"";s:9:"saleprice";s:0:"";}i:3;a:3:{s:6:"option";s:0:"";s:5:"price";s:0:"";s:9:"saleprice";s:0:"";}}s:11:"description";s:124:"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";s:8:"shiprate";s:1:"A";s:8:"featured";s:2:"no";s:4:"sale";s:3:"yes";s:10:"cart_radio";s:1:"0";s:6:"optset";s:0:"";}

This is from a wordpress site, I need to retrieve the first "price" value which is 14.95 what is the best way to retrieve a specific value from a serialized array stored in mysql?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姐不稀罕 2024-12-14 05:59:57

创建查询,从 SQL 读取数据,unserialize() metaValue 并像数组一样访问它。如果需要在 mysql 中提取它,也可以尝试使用正则表达式,但这不是一个很好的方法。

Create query, read data from SQL, unserialize() metaValue and access it like an array. You can also try to use regular expressions if you need to extract it inside mysql, but that's a not very good approach.

单身狗的梦 2024-12-14 05:59:57

使用 unserialize() 检索 metaValue 数据。

以下代码可以检索第一个价格值:

$result = mysql_query("SELECT * FROM tbl WHERE id=55");
$row = mysqLfetch_assoc($result);

$serial = $row['metaValue'];
$data = unserialize($serial);
echo $data['products'][1]['price'];

该行中的数据结构如下:

array(8) {
  ["sku"]=>
  string(0) ""
  ["products"]=>
  array(3) {
    [1]=>
    array(3) {
      ["option"]=>
      string(1) "1"
      ["price"]=>
      string(5) "14.95"
      ["saleprice"]=>
      string(0) ""
    }
    [2]=>
    array(3) {
      ["option"]=>
      string(0) ""
      ["price"]=>
      string(0) ""
      ["saleprice"]=>
      string(0) ""
    }
    [3]=>
    array(3) {
      ["option"]=>
      string(0) ""
      ["price"]=>
      string(0) ""
      ["saleprice"]=>
      string(0) ""
    }
  }
  ["description"]=>
  string(124) "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
  ["shiprate"]=>
  string(1) "A"
  ["featured"]=>
  string(2) "no"
  ["sale"]=>
  string(3) "yes"
  ["cart_radio"]=>
  string(1) "0"
  ["optset"]=>
  string(0) ""
}

Use unserialize() to retrieve the metaValue data.

The following code can retrieve the first price value:

$result = mysql_query("SELECT * FROM tbl WHERE id=55");
$row = mysqLfetch_assoc($result);

$serial = $row['metaValue'];
$data = unserialize($serial);
echo $data['products'][1]['price'];

The data in this row is structured as:

array(8) {
  ["sku"]=>
  string(0) ""
  ["products"]=>
  array(3) {
    [1]=>
    array(3) {
      ["option"]=>
      string(1) "1"
      ["price"]=>
      string(5) "14.95"
      ["saleprice"]=>
      string(0) ""
    }
    [2]=>
    array(3) {
      ["option"]=>
      string(0) ""
      ["price"]=>
      string(0) ""
      ["saleprice"]=>
      string(0) ""
    }
    [3]=>
    array(3) {
      ["option"]=>
      string(0) ""
      ["price"]=>
      string(0) ""
      ["saleprice"]=>
      string(0) ""
    }
  }
  ["description"]=>
  string(124) "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
  ["shiprate"]=>
  string(1) "A"
  ["featured"]=>
  string(2) "no"
  ["sale"]=>
  string(3) "yes"
  ["cart_radio"]=>
  string(1) "0"
  ["optset"]=>
  string(0) ""
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文