Blogger Information
Blog 5
fans 0
comment 0
visits 4454
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
for()循环中增量
月影的博客
Original
1066 people have browsed it

问题:

        $arr1 = ['name','php','birth','1995','latest','1.0'];

        $arr2 = ['name'=>'php','birth'=>'1995','latest'=>'1.0'];

        封装一个方法,可以把数组1变成数组2;

一:

   function test($arr1){

        if (count($arr1) % 2 != 0) {

            return; //数组个数为奇数则返回

        }

        for ($i=0; $i < count($arr1); $i += 2) {

            $arr2[$arr1[$i]] = $arr1[$i+1];

        }

        return $arr2;

    }

二:

    

function ccc($arr1) {

        $arr2 = [];

        foreach ($arr1 as $key => $value) {

            if ($key % 2 == 0) {

                if (isset($arr1[$key + 1])) {

                    $next = $arr1[$key + 1];

                } else {

                    $next = '';

                }

                $arr2[$value] = $next;

            }

        }

        dump($arr2);

    }


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments