Home  >  Article  >  Backend Development  >  PHP changes a loop traversal.

PHP changes a loop traversal.

WBOY
WBOYOriginal
2016-08-04 09:19:351076browse

<code>    $imagee = $_GET['pic'];
        foreach ($imagee as $k => $v) {
         $image[]= array(
                'image_src'  =>$v,
                'image_name' => mt_rand(1, 1111) . '.jpg'
            );
        }</code>

This can only be read one by one. Multiple pics must be submitted to the front desk
The format of each pic is: http://www.baidu.com/1.jpgor gif
I used another method to get all The image address, the format is as follows

<code> http://www.baidu.com/1.jpg分隔符http://www.baidu.com/2.jpg分隔符http://www.baidu.com/3.jpg</code>

How to make it traverse into the style I want in php?

Reply content:

<code>    $imagee = $_GET['pic'];
        foreach ($imagee as $k => $v) {
         $image[]= array(
                'image_src'  =>$v,
                'image_name' => mt_rand(1, 1111) . '.jpg'
            );
        }</code>

This can only be read one by one. Multiple pics must be submitted to the front desk
The format of each pic is: http://www.baidu.com/1.jpgor gif
I used another method to get all The image address, the format is as follows

<code> http://www.baidu.com/1.jpg分隔符http://www.baidu.com/2.jpg分隔符http://www.baidu.com/3.jpg</code>

How to make it traverse into the style I want in php?

Traverse to the style you want? What style. If I guessed what you meant wrong, then explode?


<code>//这里假设分割符是__(两个英文下划线)
$pic = "http://www.baidu.com/1.jpg__http://www.baidu.com/2.jpg__http://www.baidu.com/3.jpg";
$arr = explode('__', $pic);

foreach($arr as $key => $v) {
    unset($arr[$key]);
    $arr[$key]['image_src'] = $v;
    $arr[$key]['image_name'] = mt_rand(1, 1111).'.jpg';
}
print_r($arr);</code>

I don’t know if I didn’t understand the meaning, but it feels like it’s not much different from the original poster’s code, it just has one more explode

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn