0
$array = array('username' => 'mainytype','password' => 'abc1234','phone' =>     '7023451287');
$count = 0;

foreach($array as $k => $v){
   $count++;
   $new_data['info'.$count] = array("input[$k]=".$v);
}


print_r($new_data);

$ty = http_build_query($new_data);
print_r($ty);

?>

// print_r($new_data):
 Array ( [info1] => Array ( [0] => input[username]=mainytype ) [info2] => Array ( [0]     => input[password]=abc1234 ) [info3] => Array ( [0] => input[phone]=7023452134 ) ) 

//print_r($ty);
info1%5B0%5D=input%5Busername%5D%3Dmainytype&info2%5B0%5D=input%5Bpassword    %5D%3Dabc1234&info3%5B0%5D=input%5Bphone%5D%3D7023452134

I guess that foreach builds 3 separated arrays. I understand the result of http_build_query(), but I would like it to look like this:

input%5Busername%5D=mainytype&input%5Bpassword%5D=abc1234&input%5Bphone%5D=7023451287

2 Answers 2

2

Try this

<?php
$array = array('username' => 'mainytype','password' => 'abc1234','phone' =>     '7023451287');

foreach($array as $k => $v){
  $new_data["input[$k]"] = $v;
}


print_r($new_data);

$ty = http_build_query($new_data);
echo($ty);

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

Comments

1

This should give you the results you need:

$array = array(
    'username' => 'mainytype',
    'password' => 'abc1234',
    'phone'    => '7023451287'
);

$new_data = array('input' => $array);

print_r($new_data);

$ty = http_build_query($new_data);
echo($ty);

http_build_query has no problems with nested arrays.

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.