【发布时间】:2015-03-08 13:28:43
【问题描述】:
字段图像接受这种格式的多图像导入:
a:10:{i:0;
a:1:{s:6:"imgurl";s:71:"http://example.com/wp-content/uploads/2015/01/image.jpg";}
i:1;
a:1:{s:6:"imgurl";s:71:"http://example.com/wp-content/uploads/2015/01/image.jpg";}
};
我正在尝试找到一种方法来返回带有上述输出的 json_encode。
在转换为数组格式的过程中,我无法弄清楚a:10:{}。
$arr = array('a' => 10, // <-- I have a problem here what to use
array( 'i' => 0, 'a' => 1,
array('s' => [6, 'imgurl'], // <-- I have a problem here what to use
's' => (71,"http://example.com/wp-content/uploads/2015/01/image.jpg")
)
)
);
我这样做对吗?
更新:
抱歉,我不能更早地回复。因为我正在研究序列化和反序列化的工作原理。
有点历史,我是使用 WP All Import- Import XML / CSV WordPress 插件来导入 XML 数据的。
WP 自定义帖子类型property 包含存储图像的字段名称_property_slider_image。将 XML 容器标准拖放到该字段将不起作用。不工作,它没有链接每个属性下载的图像。
检查 mysql 数据库后,该字段接受我上面提到的这种类型的合成器。老实说,我不知道它是什么。因为我只知道json_encode和json_decode。这就是为什么我的帖子提到 json_encode。
感谢Kiyan。他给了我一个提示。
现在我不会手动将每个图像映射到每个属性记录。
结果:
这是我经过数周研究后的成果。
function sl_output_property_slider(){
$image_url_list = array();
foreach (func_get_args() as $n) {
// get image name
$img_name = get_image_name($n);
// add directory location with the following format
// http://localhost/dev_slrealestate/wp-content/uploads/2015/01/1478_Image.jpeg
$imgurl = 'http://localhost/dev_site/wp-content/uploads/'. date('Y') .'/'. date('m') . '/' . $img_name;
array_push($image_url_list, array('imgurl'=>$imgurl));
}
$serialized_data = serialize($image_url_list);
printf($serialized_data);
}
其中function get_image_name($url) - 仅从原始 url 字符串返回图像名称。
示例用法 - 短代码
[sl_output_property_slider({Images[1]/Image[1]/ImageURL[1]},
{Images[1]/Image[2]/ImageURL[1]},{Images[1]/Image[3]/ImageURL[1]},
{Images[1]/Image[4]/ImageURL[1]},{Images[1]/Image[5]/ImageURL[1]},
{Images[1]/Image[6]/ImageURL[1]},{Images[1]/Image[7]/ImageURL[1]},
{Images[1]/Image[8]/ImageURL[1]},{Images[1]/Image[9]/ImageURL[1]},
{Images[1]/Image[10]/ImageURL[1]}
)
]
【问题讨论】:
-
你有什么问题?!没看懂,太不清楚了!您是否搜索输入以获得第一个代码框的输出?!
-
作为最后的手段,我采用逆向工程风格的解决方案。你不知何故正确。我检查了这个表和字段所在的数据库。我提到的合成器是它的存储方式。在 WP All Import- Import XML / CSV - Custom Field - 你有两个选项来放置 XML 容器。 1.] 序列化 2.] 映射。序列化是最好的选择。但是我已经浪费了太多时间尝试不同的组合来将
\ {Images[1]/Image[10]/ImageURL[1]}例如放置到_property_slider没有成功。
标签: php xml json serialization wordpress