How to turn array into string with php

Asked

Viewed 1,666 times

1

item 1 - How do I do that

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

    [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 - each set in a row)

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

being the 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));

2 answers

3

You can use the function implode().

implode($array, ',');

This will cause the array to be grouped into a string separating the values by 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

1


Is a array which 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

  • Isn’t that what you want? @Leocaracciolo who generates this data is the function you have in your question

  • Or do you want each one to fall a line? @Leocaracciolo

  • exact, each in a row

  • a line 2,4,5,6,7,9,10,12,15,16,18,20,21,22,23 second line 2,4,5,6,7,9,10,12,15,1 6,18,20,21,22,24 Or a different comma separator for each group of 15 tens

  • Ready @Leocaracciolo,,,,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.