Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

php - How to import multiple image?

Field image is accepting this type of format for multiple image import:

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

I am trying to find a way to return a json_encode with the above output. In the process I can't figure out a:10:{} when converted to array format.

$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")
                               )
                         )
        );

Am I doing this correctly?

Update:

Sorry I can't response sooner. Since I'm studying how serialize and unserialize works.

A bit of history, I am using WP All Import- Import XML / CSV WordPress plugin to import XML data.

The WP custom post type property contain a field name _property_slider_image that store an image(s). Standard drag and drop of XML container to that field won't work. Not working as, it's not linking the downloaded images per property.

After checking the mysql database the field accept this type of synthax I mention above. I honestly do not know what it is. Since I only know is json_encode and json_decode. That is why my post mention json_encode.

Thanks to Kiyan. He gave me a hint where to look.

Now I won't be manually mapping each image to each property record.

Result:

Here's my output after a weeks of research.

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);
}

where function get_image_name($url) - to return the image name only from original url string.

Sample Usage - short code

[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]}
 )
]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

this is not a json string, you have to use unserialize function

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...