3

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.

7
  • Do you want to convert entire array in single string? Can you please add example for it. Commented Dec 3, 2015 at 6:07
  • Have you tried $var = print_r($myArray, true);? Commented Dec 3, 2015 at 6:07
  • @HtmHell, his given above code looks like output of print_r. How print_r($myArray) will help to convert entire array in string? Commented Dec 3, 2015 at 6:09
  • @SatishSojitra I have edited my comment. Commented Dec 3, 2015 at 6:10
  • @Uchiha This is the output of json_decode Commented Dec 3, 2015 at 6:13

5 Answers 5

7

You can simply use array_walk_recursive like as

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

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

Comments

0

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.

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.