gpt4 book ai didi

php - 在mysql while循环中解析序列化数据

转载 作者:行者123 更新时间:2023-11-29 10:50:47 24 4
gpt4 key购买 nike

我正在尝试解析 wp_postmeta 表中的 WordPress meta_value 列。

meta_value 行内容采用序列化格式,如下所示:

a:1:{i:0;O:8:"stdClass":2:{s:5:"notes";s:0:"";s:12:"applications";a:5:
{s:4:"Year";O:8:"stdClass":2:
{s:4:"name";s:4:"Year";s:5:"value";s:4:"2006";}s:4:"Make";O:8:"stdClass":2:
{s:4:"name";s:4:"Make";s:5:"value";s:5:"Isuzu";}s:5:"Model";O:8:"stdClass":2:
{s:4:"name";s:5:"Model";s:5:"value";s:5:"i-280";}s:4:"Trim";O:8:"stdClass":2:
{s:4:"name";s:4:"Trim";s:5:"value";s:31:"Base Extended Cab Pickup 2-Door";}s:6:"Engine";O:8:"stdClass":2:
{s:4:"name";s:6:"Engine";s:5:"value";s:54:"2.8L 2770CC 169Cu. In. l4 GAS DOHC Naturally Aspirated";}}}}

我能够使用 while 循环来反序列化数据

$results = $mysqli->query("SELECT meta_value
FROM wp_postmeta
WHERE meta_key = '_ebay_item_compatibility_list';");

while($row = $results->fetch_array()) {
echo '<pre>';
print_r(unserialize($row['meta_value']));
echo '</pre>';
}

这给了我类似的输出

Array
(
[0] => stdClass Object
(
[notes] => ONLY FITS 6 LUG VEHICLES
[applications] => Array
(
[Year] => stdClass Object
(
[name] => Year
[value] => 2008
)

[Make] => stdClass Object
(
[name] => Make
[value] => Toyota
)

[Model] => stdClass Object
(
[name] => Model
[value] => Tacoma
)

[Trim] => stdClass Object
(
[name] => Trim
[value] => Base Crew Cab Pickup 4-Door
)

[Engine] => stdClass Object
(
[name] => Engine
[value] => 2.7L 2694CC l4 GAS DOHC Naturally Aspirated
)

)

)

[1] => stdClass Object
(
[notes] => ONLY FITS 6 LUG VEHICLES
[applications] => Array
(
[Year] => stdClass Object
(
[name] => Year
[value] => 2008
)

[Make] => stdClass Object
(
[name] => Make
[value] => Toyota
)

[Model] => stdClass Object
(
[name] => Model
[value] => Tacoma
)

[Trim] => stdClass Object
(
[name] => Trim
[value] => Base Crew Cab Pickup 4-Door
)

[Engine] => stdClass Object
(
[name] => Engine
[value] => 4.0L 3956CC 241Cu. In. V6 GAS DOHC Naturally Aspirated
)

)

)

我想做的是访问所有value元素。

含义类似于 $year = ['Year']['value'];$make = ['Make']['value']; 等.

如何在 while 循环中访问这些 value 元素?

最佳答案

您有 stdClass 对象和数组的组合,因此对于数组,您通常使用 [] 和对象的属性 ->

示例:

while($row = $results->fetch_array()) {
$foo = unserialize($row['meta_value']);
$foo[0]->applications['Make']->value;

}

编辑:如果一行中有更多数组元素,可以使用 foreach

while($row = $results->fetch_array()) {
$foo = unserialize($row['meta_value']);
foreach ( $foo as $f) {
foreach ( $f->applications as $application) {
echo $application->value;
}
}
}

关于php - 在mysql while循环中解析序列化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43768318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com