在 PHP 中从序列化数组中获取一个值

发布于 2024-12-16 15:07:14 字数 164 浏览 0 评论 0原文

您认为从数组中获取单个值的最有效方法是什么。我知道它是什么,我知道它在哪里。目前我正在这样做:

 $array = unserialize($storedArray);
 $var = $array['keyOne'];

想知道是否有更好的方法。

What would you say is the most efficient way to get a single value out of an Array. I know what it is, I know where it is. Currently I'm doing it with:

 $array = unserialize($storedArray);
 $var = $array['keyOne'];

Wondering if there is a better way.

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

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

发布评论

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

评论(6

初雪 2024-12-23 15:07:14

你做得很好,我想不出比你所做的更好的方法了。

  • 您反序列化
  • 您得到一个数组
  • 您通过指定索引获得值

这就是它可以完成的方式。

You are doing it fine, I can't think of a better way than what you are doing.

  • You unserialize
  • You get an array
  • You get value by specifying index

That's the way it can be done.

甩你一脸翔 2024-12-23 15:07:14

想知道是否有更好的方法。

对于您给出的数组示例,我认为您没问题。

如果序列化字符串包含您不想反序列化的数据和对象(例如创建您确实不想拥有的对象),您可以使用 序列化 PHP 库,它是序列化数据的完整解析器。

它提供对序列化数据的静态低级访问,因此您只能提取数据的子集和/或操作序列化数据而不对其进行反序列化。然而,对于您的示例来说,这看起来太多了,因为您只有一个数组,而且我猜您不需要过滤/差异太多。

Wondering if there is a better way.

For the example you give with the array, I think you're fine.

If the serialized string contains data and objects you don't want to unserialize (e.g. creating objects you really don't want to have), you can use the Serialized PHP library which is a complete parser for serialized data.

It offers low-level access to serialized data statically, so you can only extract a subset of data and/or manipulate the serialized data w/o unserializing it. However that looks too much for your example as you only have an array and you don't need to filter/differ too much I guess.

反差帅 2024-12-23 15:07:14

这是您可以执行的最有效的方法,反序列化并获取数据,如果您需要优化,请不要存储所有序列化的变量。

而且总有办法用正则表达式来解析它:)

Its most efficient way you can do, unserialize and get data, if you need optimize dont store all variables serialized.

Also there is always way to parse it with regexp :)

与君绝 2024-12-23 15:07:14

如果您不想取消整个事物的序列化(这可能会很昂贵,尤其是对于更复杂的对象),您可以只执行 strpos 并查找您想要的特征并提取它们

If you dont want to unseralize the whole thing (which can be costly, especially for more complex objects), you can just do a strpos and look for the features you want and extract them

我们的影子 2024-12-23 15:07:14

当然。
如果您需要更好的方法 - 不要使用序列化数组。
序列化只是一种传输格式,用途非常有限。

如果您需要一些优化的变体 - 有数百个。
例如,您可以传递一些单个标量变量而不是整个数组。并立即访问

Sure.
If you need a better way - DO NOT USE serialized arrays.
Serialization is just a transport format, of VERY limited use.

If you need some optimized variant - there are hundreds of them.
For example, you can pass some single scalar variable instead of whole array. And access it immediately

思念绕指尖 2024-12-23 15:07:14

我也认为正确的方法是取消序列化。

但另一种方法可能是使用字符串操作,当你知道你想从数组中得到什么时:

$storedArray = 'a:2:{s:4:"test";s:2:"ja";s:6:"keyOne";i:5;}';
# another: a:2:{s:4:"test";s:2:"ja";s:6:"keyOne";s:3:"sdf";}
$split = explode('keyOne', $storedArray, 2);
# $split[1] contains the value and junk before and after the value
$splitagain = explode(';', $split[1], 3);
# $splitagain[1] should be the value with type information
$value = array_pop(explode(':', $splitagain[1], 3));
# $value contains the value

现在,有人准备进行基准测试吗? ;)
另一种方式可能是RegEx

I, too, think the right way is to un-serialize.

But another way could be to use string operations, when you know what you want from the array:

$storedArray = 'a:2:{s:4:"test";s:2:"ja";s:6:"keyOne";i:5;}';
# another: a:2:{s:4:"test";s:2:"ja";s:6:"keyOne";s:3:"sdf";}
$split = explode('keyOne', $storedArray, 2);
# $split[1] contains the value and junk before and after the value
$splitagain = explode(';', $split[1], 3);
# $splitagain[1] should be the value with type information
$value = array_pop(explode(':', $splitagain[1], 3));
# $value contains the value

Now, someone up for a benchmark? ;)
Another way might be RegEx ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文