【问题标题】:How to import multiple image?如何导入多张图片?
【发布时间】: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


【解决方案1】:

正如 Kiyan 所说,您在 jsonserialize 格式之间感到困惑。

您给出的数组表示可以使用:

$arr = array(
    array('imgurl' => 'http://example.com/wp-content/uploads/2015/01/image.jpg'),
    array('imgurl' => 'http://example.com/wp-content/uploads/2015/01/image.jpg'),
);

$serialized = serialize($arr);

例子:

echo $serialized;

给予:

a:2:{i:0;a:1:{s:6:"imgurl";s:55:"http://example.com/wp-content/uploads/2015/01/image.jpg";}i:1;a:1:{s:6:" imgurl";s:55:"http://example.com/wp-content/uploads/2015/01/image.jpg";}}

【讨论】:

  • 是的,我意识到在 Kiyan 提到反序列化之后。在阅读您的答案之前,我已经弄清楚谁使用了序列化功能。
  • 这是最重要的 :-)。
  • 使用printf($serialized_data) 代替return $serialized_data 作为返回有点不寻常
  • 不使用标准序列化(xml、json、yml...)在多个应用程序之间进行通信有点不寻常:-)
【解决方案2】:

这不是一个json字符串,你必须使用反序列化函数

http://php.net/manual/en/function.unserialize.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 2019-05-24
    • 2018-05-06
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多