如何导入多张图片


How to import multiple image?

字段图像接受这种类型的格式进行多图像导入:

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所有导入 - 导入XML/CSV WordPress插件来导入XML数据。

WP自定义帖子类型property包含存储图像的字段名称_property_slider_image。将 XML 容器标准拖放到该字段将不起作用。不起作用,因为它没有链接每个属性的下载图像。

检查 mysql 数据库后,该字段接受我上面提到的这种类型的合成器。老实说,我不知道它是什么。因为我只知道是json_encode和json_decode。这就是为什么我的帖子提到json_encode。

感谢基扬。他给了我一个提示,让我去哪里看。

现在,我不会手动将每个图像映射到每个属性记录。

结果:

这是我经过一周研究后的结果。

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]}
 )
]
这不是

JSON 字符串,你必须使用反序列化函数

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

正如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";}}