Say you have an array like this...
假设你有一个这样的数组……
username, password, email
and you need to assign a value to each element. After, this needs to be formatted into a string like this:
你需要给每个元素分配一个值。之后,需要将其格式化为如下字符串:
username=someRandomValueAssigned&password=someRandomValueAssigned&email=someRandomValueAssigned
how would I do this? Thanks.
我该怎么做呢?谢谢。
4 个解决方案
#1
3
$keys = array('username', 'password', 'email');
$values = array('someusername', 'somepassword', 'someemail');
$data = array_combine($keys, $values);
array_combine will return an associative array like,
array_combine会返回一个关联数组,
$data = array(
'username' => 'someusername',
'password' => 'somepassword',
'email' => 'someemail'
);
then the result you want can be achieved using a simple foreach loop
然后可以使用一个简单的foreach循环来实现所需的结果
$str = '';
foreach($data as $k=>$v) {
$str .= $k > 0 ? '&' : '';
$str .= $k . '=' . $v ;
}
echo $str;
Also, I suspect you are trying to build a url so you might want to check out php's http_build_query function
另外,我怀疑您正在尝试构建一个url,以便您可能希望检查php的http_build_query函数
#2
1
$array_value=array();
$array_value['username']=somevalue;
$array_value['password']=somevalue;
$array_value['email']=somevalue;
$array_str=array();
foreach($array_value as $key=>$value){
array_push($array_str,$key."=".$array_value[$value]);
}
$array_str=join("&",$array_str);
echo $array_str;
#3
1
Looks like you're building a query string, I think you want to use http_build_query()
:
看起来您正在构建一个查询字符串,我认为您希望使用http_build_query():
$data = array(
'username' => 'someRandomValueAssigned',
'password' => 'someRandomValueAssigned',
'email' => 'someRandomValueAssigned',
);
$query_string = http_build_query($data);
This should give you the result you're looking for.
这会给你你想要的结果。
http_build_query — Generate URL-encoded query string
http_build_query -生成url编码的查询字符串。
http://php.net/manual/function.http-build-query.php
http://php.net/manual/function.http-build-query.php
#4
0
$randomValue = array('username' => 'someValue' ); // same for other
foreach($array as &$value){
$value = $value.'='.$randomValue[$value];
}
echo implode('&', $array);
#1
3
$keys = array('username', 'password', 'email');
$values = array('someusername', 'somepassword', 'someemail');
$data = array_combine($keys, $values);
array_combine will return an associative array like,
array_combine会返回一个关联数组,
$data = array(
'username' => 'someusername',
'password' => 'somepassword',
'email' => 'someemail'
);
then the result you want can be achieved using a simple foreach loop
然后可以使用一个简单的foreach循环来实现所需的结果
$str = '';
foreach($data as $k=>$v) {
$str .= $k > 0 ? '&' : '';
$str .= $k . '=' . $v ;
}
echo $str;
Also, I suspect you are trying to build a url so you might want to check out php's http_build_query function
另外,我怀疑您正在尝试构建一个url,以便您可能希望检查php的http_build_query函数
#2
1
$array_value=array();
$array_value['username']=somevalue;
$array_value['password']=somevalue;
$array_value['email']=somevalue;
$array_str=array();
foreach($array_value as $key=>$value){
array_push($array_str,$key."=".$array_value[$value]);
}
$array_str=join("&",$array_str);
echo $array_str;
#3
1
Looks like you're building a query string, I think you want to use http_build_query()
:
看起来您正在构建一个查询字符串,我认为您希望使用http_build_query():
$data = array(
'username' => 'someRandomValueAssigned',
'password' => 'someRandomValueAssigned',
'email' => 'someRandomValueAssigned',
);
$query_string = http_build_query($data);
This should give you the result you're looking for.
这会给你你想要的结果。
http_build_query — Generate URL-encoded query string
http_build_query -生成url编码的查询字符串。
http://php.net/manual/function.http-build-query.php
http://php.net/manual/function.http-build-query.php
#4
0
$randomValue = array('username' => 'someValue' ); // same for other
foreach($array as &$value){
$value = $value.'='.$randomValue[$value];
}
echo implode('&', $array);