如何序列化数组使用PHP(数组查询字符串) [英] How to serialize an array (array to query string) using PHP

查看:181
本文介绍了如何序列化数组使用PHP(数组查询字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据数组来自$ _ POST,我想通过卷曲送他们到另一个页面。

  curl_setopt($ S,CURLOPT_POST,真正的);
 curl_setopt($ S,CURLOPT_POSTFIELDS,$这个 - > _postFields);

$这个 - > _postFields 必须是一个字符串,如 A = 2和b = T 吧?

所以如果我想与卷曲发送数据的阵列到另一个页面,我必须把数组查询字符串吧?

我应该如何使用PHP做呢?

♦我试过连载()反序列化()但thier格式不一样的查询字符串吧?

所以我该怎么做? (我需要像 .serialize() jQuery中,关于阵列工作不能形成)

♦和目标路径是不是我的控制之下,而$ _ POST在目标应该是为$ _POST不为BASE64 EN $ C $的CD,所以我不能用这样codeS。

  $阵列=阵列(1,2,3);
$连接codeD = json_en code($数组);
$德codeD = json_de code($连接codeD);
的print_r($德codeD);

任何想法?

先谢谢了。


解决方案

您可以使用的 http_build_query

  curl_setopt($ S,CURLOPT_POSTFIELDS,http_build_query($这个 - > _postFields));


  

注意


看起来美丽,但使用这种方法要小心URL编码。看着旁边:

  $ _ POST [一些价值] ='值1'; //之间
$ _ POST [下一个值] ='值'; //之间$ URL = http_build_query($ _ POST);回声$网址;//输出
某些+值=值1&放大器;下一个+价值= VALUE2

当然,发送此$网址后,我们不会得到预期从$ _ POST变量。

I have an array of data comes from $_POST and I want to send them by curl to another page.

 curl_setopt($s,CURLOPT_POST,true);
 curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields);

$this->_postFields must be an string like a=2&b=t right?
so if I want to send array of data with curl to another page I must turn array to query string right?

How should I do it with PHP?

♦I tried serialize() and unserialize() but thier format is not same as query string right?
so what should I do? (I need something like .serialize() in jQuery that work on array not FORM)

♦ And the destination path is not under my control and the $_POST in the destination should be as $_POST not as its base64 encoded so I can't use such codes.

$array = array(1,2,3);
$encoded = json_encode($array);
$decoded = json_decode($encoded);
print_r($decoded);

Any Ideas?

thanks in advance.

解决方案

You can use http_build_query:

curl_setopt($s, CURLOPT_POSTFIELDS, http_build_query($this->_postFields));

NOTICE

It looks beauty, but to use this approach be careful about url encoding .Look next:

$_POST["some value"]='value1'; // " " between
$_POST["next value"]='value2'; // " " between

$url = http_build_query($_POST);

echo $url;

// OUTPUT 
some+value=value1&next+value=value2

Of course, after sending this $url we will not get expected variables from $_POST.

这篇关于如何序列化数组使用PHP(数组查询字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆