How to transform array into string with php

1

item 1 - How do I do this?

 Array
 (
    [0] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 5
            [3] => 6
            [4] => 7
            [5] => 9
            .......
            .......
        )

    [1] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 5
            [3] => 6
            [4] => 7
            [5] => 9
            ........
            ........
        )
    [2] => Array
        (
            .....
            .....

item 2 - turn this (15 numbers separated by commas)

2,4,5,6,7,8,...
2,4,5,6,7,9,...
...............
...............

being item 1 result of print_r(combinacoesDe(15,$dezenas)); thereof

$dezenas = array("2", "4", "5", "6", "7", "9", "10", "12", "15", "16", "18", "20", "21", "22", "23", "24", "25");

function combinacoesDe($k, $xs){

        if ($k === 0)
            return array(array());
        if (count($xs) === 0)
            return array();
        $x = $xs[0];

        $xs1 = array_slice($xs,1,count($xs)-1);

        $res1 = combinacoesDe($k-1,$xs1);

        for ($i = 0; $i < count($res1); $i++) {
            array_splice($res1[$i], 0, 0, $x);
        }
        $res2 = combinacoesDe($k,$xs1);

        return array_merge($res1, $res2);

 }

print_r(combinacoesDe(15,$dezenas));
    
asked by anonymous 05.08.2017 / 04:09

2 answers

2

It is a array that has positions and within each position another array , example

$dezenas = array("2", "4", "5", "6", "7", "9", "10", "12", "15", 
"16", "18", "20", "21", "22", "23", "24", "25");

function combinacoesDe($k, $xs){

        if ($k === 0)
            return array(array());
        if (count($xs) === 0)
            return array();
        $x = $xs[0];

        $xs1 = array_slice($xs,1,count($xs)-1);

        $res1 = combinacoesDe($k-1,$xs1);

        for ($i = 0; $i < count($res1); $i++) {
            array_splice($res1[$i], 0, 0, $x);
        }
        $res2 = combinacoesDe($k,$xs1);

        return array_merge($res1, $res2);

 }

$result = combinacoesDe(15,$dezenas);

foreach($result as $key => $value)
{
    echo implode($value,',');
    echo PHP_EOL;
}

Online Example

    
05.08.2017 / 04:24
2

You can use the implode() function.

implode($array, ',');

This will cause the array to be grouped into a string by separating the values with a comma (you can use anything as a separator).

Example:

$array = [1,2,3];
$i = implode($array, ',');
var_dump($i);
// Resultado 1,2,3

$i = implode($array, '@');
var_dump($i);
// Resultado 1@2@3
    
05.08.2017 / 04:12