Search code examples
phparraysmultidimensional-arrayimplode

Php How to make a multidimensional array as a single string


array(array("Color", "red" ), array("Ram", "4GB" ) );

Convert this multidimensional array into a string like this

Color=red&Ram=4GB

Solution

  • You need to rearrange the input array a bit and then you can use http_build_query()

    $sq = array(array("Color", "red" ), array("Ram", "4GB" ) );
    
    $aa = [];
    
    foreach( $sq as $s ) {
        $aa[$s[0]] = $s[1];
    }
    
    echo http_build_query($aa);
    

    RESULT

    Color=red&Ram=4GB