mega6382
mega6382

Reputation: 9396

Multidimensional Array to String conversion

I have a dynamic multidimensional array and I want to convert it to string. here is an example:

Array
(
[data] => check
[test1] => Array
    (
        [data] => Hello
    )

[test2] => Array
    (
        [data] => world
    )

[test3] => Array
    (
        [data] => bar
        [tst] => Array
            (
                [data] => Lorem
                [bar] => Array
                    (
                        [data] => doller
                        [foo] => Array
                            (
                                [data] => sit
                            )

                    )

            )

    )

[test4] => Array
    (
        [data] => HELLO
        [tst] => Array
            (
                [data] => ipsum
                [bar] => Array
                    (
                        [data] => Lorem
                    )

            )

    )

)

The example for string is:

 check&hello&world&bar...lorem&doller...sit ....

I have tried alot of things. I even checked the solutions given on other SO questions. like: Convert Multidimensional array to single array & Multidimensional Array to String But No luck.

Upvotes: 3

Views: 4525

Answers (5)

Satish Sojitra
Satish Sojitra

Reputation: 632

You can use following recursive function to convert any multidimensional array to string

 public function _convertToString($data,&$converted){
    foreach($data as $key => $value){
        if(is_array($value)){
                $this->_convertToString($value,$converted);

        }else{
            $converted .= '&'. $value;
        }
    }
}

You can call above function in following way:

   $str = array(
      "data" => "check",
        "test1" => array(
          "data" => "Hello",
            "test3" => array(
                "data" => "satish"
            )
        ),
        "test2" => array(
            "data" => "world"
        )
    );

    $converted = "";

    //call your function and pass your array and reference string
    $this->_convertToString($str,$converted);

    echo $converted;

Output will be following:

check&Hello&satish&world

you can modify code to meet your requirement.

Let me know if any further help required.

Upvotes: 1

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

You can simply use array_walk_recursive like as

$result = [];
array_walk_recursive($arr, function($v) use (&$result) {
    $result[] = $v;
});
echo implode('&', $result);

Demo

Upvotes: 7

thepiyush13
thepiyush13

Reputation: 1331

I would use recursion for this type of array :

echo visit($your_array);

function visit($val){
    if( !is_array($val) ){
        return $val;
    }
    $out="";
    foreach($val as $v){
       $out.= "&".visit($v);
    }
    return $out;
}

Upvotes: 0

viral
viral

Reputation: 3743

First convert it to flat array, by

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($input_array));
$flat = iterator_to_array($it, false);

false prevents array key collision.

Then use implode,

$str = implode('&', $flat);

Upvotes: 1

Niek de Gooijer
Niek de Gooijer

Reputation: 684

php has some build in functions that can do this. Like var_dump, json_encode and var_export. But if you want to control the output more it can be doen with a recursive function

function arrToStr(array $data)
{
    $str = "";
    foreach ($data as $val) {
        if (is_array($val)) {
            $str .= arrToStr($val);
        } else {
            $str .= $val;
        }
    }

    return $str;
}

You can format extra line breaks and spaces at will with this.

Upvotes: 0

Related Questions