简体   繁体   English

如何在Smarty中反序列化tpl文件中的JSON数据?

[英]How to unserialize a JSON data in a tpl file in Smarty?

I need your help with my problem. 我需要我的帮助。 I am new in smarty and I am having a hard time finding a solution with my problem. 我是聪明人中的新手,很难解决我的问题。 Ok here's my problem. 好的,这是我的问题。

I have an array that is formatted to JSON and it is serialize. 我有一个格式化为JSON的数组,并且已序列化。 What I want is to unserialize it and extract the array value. 我想要的是将其反序列化并提取数组值。

What I did is I assign the serialize data into a variable and the next is unserialize it but it doesn't display my data. 我要做的是将序列化数据分配给一个变量,接下来对它进行反序列化,但是它不显示我的数据。

Here's my code: 这是我的代码:

{assign var="type" value="$product.employment_type"}
{$type|unserialize} {**doesn't display my data**}

Here's my sample output in serialize: 这是我的序列化示例输出:

a:2:{i:0;s:8:"fulltime";i:1;s:8:"contract";}

Here's the array form: 这是数组形式:

Array
(
    [0] => fulltime
    [1] => contract
)

The next problem is display the array values. 下一个问题是显示数组值。

My sample output should be: 我的示例输出应为:

fulltime, contract

That's all guys I hope you can help me. 这些都是我希望您能帮助我的人。

Assuming that $product.employment_type returns the string "a:2{i:0;s:8:"fulltime";i:1;s:8:"contract";}", you could try the following: 假设$ product.employment_type返回字符串“ a:2 {i:0; s:8:“全职”; i:1; s:8:“ contract”;}“,则可以尝试以下操作:

{assign var=type value=$product.employment_type|unserialize}
{$type.0}, {$type.1}

Or you could also try: 或者,您也可以尝试:

{foreach from=$product.employment_type|unserialize item=type}
    {$type}
{/foreach}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM