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

Convert this multidimensional array into a string like this

Color=red&Ram=4GB
4
  • 1
    Its always worth a search before asking Commented May 8, 2021 at 18:37
  • Its not a key value pair thats the problem Commented May 8, 2021 at 18:41
  • @RiggsFolly its not a key value pair thats the problem Commented May 8, 2021 at 18:44
  • Loop over it, have a new array and make the key value pair thing and use http_build_query in the end. Commented May 8, 2021 at 18:53

3 Answers 3

3

As long as 0 is the key name and 1 is the value, then just create a single dimension indexed on the first element and build the query string:

$string = http_build_query(array_column($array, 1, 0));
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

That worked thank you soo much ☺️☺️👍👍👍
-1

Simple approach, Loop over $params and again loop for each $param then get $key $value pair concatenate them with = and store them in an array $query. last but not least join them together with &.

$params = [['color'=>'red'],['RAM'=>'4gb']];

$query = [];
foreach ($params as $param) {
    foreach ($param as $key => $value) {
        array_push($query, "$key=$value");
    }
}
echo join("&",$query);

Result:

color=red&RAM=4gb

1 Comment

Thats not a key value pair both are values

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.