0

I have an array like

$value = array('key1'=>'value1','key2'=>'value2','key3'=>'value3');

I want to convert this array to the following string.

key1=value1&key2=value2&key3=value3

I have tried with implode function, but it returns only values of the array

implode($value,"&"); // returns value1&value2&value3

Is there any simple way to achieve this ?

3
  • 1
    That's not a multidimensional array. It's just a one dimensional associative array. Anyway, you should checkout http_query_builder(). That will do what you want. Commented Jun 25, 2018 at 9:26
  • 1
    use http_build_query($arr); Commented Jun 25, 2018 at 9:27
  • Hi Magnus & Sudhir thanks for your help. Commented Jun 25, 2018 at 9:42

1 Answer 1

2

Implode is used only on array values. It doesnot work on array keys. If you want to use it for building the query string then you can use http_build_query function in php.

$value = array('key1'=>'value1','key2'=>'value2','key3'=>'value3');

echo http_build_query($value); // key1=value1&key2=value2&key3=value3

Hope this helps.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.