将两个连续值合并为一个数组中的一个double值

how i can do following array conversion in php?

// turn
array ('1', 'a', '2', 'b', '3', 'c', '4', 'd');

// into
array ('1,a', '2,b', '3,c', '4,d');

You can easily to that with array_chunk:

$original = array ('1', 'a', '2', 'b', '3', 'c', '4', 'd');

$new_array = array();
foreach(array_chunk($original, 2) as $values) {
    $new_array[] = implode(',', $values);
}

var_dump($new_array );

I'm bored:

$result = array_map(function($v) { return implode(',', $v); },
                   array_chunk($array, 2));
  • Chunk the array into child arrays of 2 values
  • Map each chunk into a function that implodes the values

I am using JavaScript but same logic applies for php too.

<script type="text/javascript">
    var list = ['1', 'a', '2', 'b', '3', 'c', '4', 'd'];

    var nlist = [];

    for(i=0; i<list.length-1; i+=2)
        nlist.push("'"+list[i]+","+list[i+1]+"'");

    alert(nlist[0]); //'1,a'
    alert(nlist); //'1,a', '2,b', '3,c', '4,d'
</script>