PHP:将数组赋值给变量
PHP: Assign array to variable
i've tried a lot of things to achieve this but none of them seem to
work. Im using PHP 7.4
Let's say i have this:
$othervar = array();
$var = array(1, 2, 3);
$othervar = $var;
THIS doesn't work for me, var_dump($othervar) returns
array(1) { [0]=> string(5) "Array" }
I've tried using array_push, i DON'T WANT to use array_merge because i
need to assign two arrays to one variable. This is what i need to do:
$variable = array();
$variable["type1"] = $data; //Array 1
$variable["type2"] = $otherData; //Array 2
This doesn't work either.
Barmar 向我展示了 here 这行得通,所以我一定是在其他地方做错了。
我将解释整个代码:
要登录到我的网页,我通过 AJAX 请求发送请求 jQuery。
function SendData(data, btn, actionOnSuccess, shouldReplace = false, elementToReplace = "", getServerData = true, htmlData = "") {
if (!loading)
{
ToggleLoading();
data.push({name: "action", value: $(btn).data("action")});
data.push({name: "attr", value: JSON.stringify($(btn).data("attr"))});
$.post("SendRequest.php", data)
.done(function (r) {
if (!r.success)
//ajax sent and received but it has an error
else
//ajax sent and it was successfull
})
.fail(function () {
//ajax call failed
});
}
else {
//This determines if some request is already executing or not.
}
}
"action" 和 "attr" 是我发送的加密值,用于引用系统上的某些操作(我将在此处显示更多信息):
代码从 AJAX 到 SendRequest.php 执行一个操作,比如登录。
SendRequest.php的第一行是:
require "Functions.php";
$apiAction = Decrypt($_POST["action"]); //Action
$actionData = json_decode(Decrypt($_POST["attr"])); //Attr
$finalPost = $_POST;
foreach ($actionData as $key => $value) { $finalPost[$key] = $value; }
$finalPost["loggedin_ip"] = $_SERVER['REMOTE_ADDR'];
$result = APICall($apiAction, $finalPost);
那么,这就是我想要与 API 进行通信的目的:
function APICall($option, $data = array())
{
session_start();
$post = array("uData" => ArrayToAPI($_SESSION), "uPost" => ArrayToAPI($data));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_URL, "https://apiurl?" . $option); //option is the ACTION to perform on API (let's say "login") it is an encrypted word on a data-attr atribute on every form/button that creates a communication with API.
$returned = curl_exec($ch);
curl_close ($ch);
$newData = json_decode($returned, true);
return $newData;
}
function ArrayToAPI($array)
{
$toApiData = array();
foreach ($array as $key=>$value) {
if (is_array($value))
$toApiData[$key] = ArrayToAPI($value);
else
$toApiData[$key] = Encrypt($value);
}
return $toApiData;
}
这就是我在 API 方面的情况:
ob_start();
var_dump($_POST);
$result = ob_get_clean();
$api->EndRequest(false, array("errorDesc" => "a - " . $result));
function EndRequest(bool $task_completed, array $data = array())
{
$jsonData = array();
$jsonData['success'] = $task_completed;
$jsonData['data'] = $data;
header('Content-type: application/json; charset=utf-8');
echo json_encode($jsonData, JSON_FORCE_OBJECT);
exit;
}
这总是 returns
array(2) { ["uData"]=> string(5) "Array" ["uPost"]=> string(5) "Array" }
我希望我现在更清楚了,谢谢。
问题在于从您的代码发出请求是因为这一行:
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
CURLOPT_POSTFIELDS
不支持多级数组。您的数组值(键所指向的)被转换为字符串,最终为 Array
。使用:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
.. 而不是 "properly" 在 post 请求中序列化多维数组("properly" 因为有很多方法可以做到这一点,但它符合预期 PHP 格式 - []
表示数组)。
i've tried a lot of things to achieve this but none of them seem to work. Im using PHP 7.4
Let's say i have this:
$othervar = array(); $var = array(1, 2, 3); $othervar = $var;
THIS doesn't work for me, var_dump($othervar) returns
array(1) { [0]=> string(5) "Array" }
I've tried using array_push, i DON'T WANT to use array_merge because i need to assign two arrays to one variable. This is what i need to do:
$variable = array(); $variable["type1"] = $data; //Array 1 $variable["type2"] = $otherData; //Array 2
This doesn't work either.
Barmar 向我展示了 here 这行得通,所以我一定是在其他地方做错了。
我将解释整个代码:
要登录到我的网页,我通过 AJAX 请求发送请求 jQuery。
function SendData(data, btn, actionOnSuccess, shouldReplace = false, elementToReplace = "", getServerData = true, htmlData = "") {
if (!loading)
{
ToggleLoading();
data.push({name: "action", value: $(btn).data("action")});
data.push({name: "attr", value: JSON.stringify($(btn).data("attr"))});
$.post("SendRequest.php", data)
.done(function (r) {
if (!r.success)
//ajax sent and received but it has an error
else
//ajax sent and it was successfull
})
.fail(function () {
//ajax call failed
});
}
else {
//This determines if some request is already executing or not.
}
}
"action" 和 "attr" 是我发送的加密值,用于引用系统上的某些操作(我将在此处显示更多信息):
代码从 AJAX 到 SendRequest.php 执行一个操作,比如登录。
SendRequest.php的第一行是:
require "Functions.php";
$apiAction = Decrypt($_POST["action"]); //Action
$actionData = json_decode(Decrypt($_POST["attr"])); //Attr
$finalPost = $_POST;
foreach ($actionData as $key => $value) { $finalPost[$key] = $value; }
$finalPost["loggedin_ip"] = $_SERVER['REMOTE_ADDR'];
$result = APICall($apiAction, $finalPost);
那么,这就是我想要与 API 进行通信的目的:
function APICall($option, $data = array())
{
session_start();
$post = array("uData" => ArrayToAPI($_SESSION), "uPost" => ArrayToAPI($data));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_URL, "https://apiurl?" . $option); //option is the ACTION to perform on API (let's say "login") it is an encrypted word on a data-attr atribute on every form/button that creates a communication with API.
$returned = curl_exec($ch);
curl_close ($ch);
$newData = json_decode($returned, true);
return $newData;
}
function ArrayToAPI($array)
{
$toApiData = array();
foreach ($array as $key=>$value) {
if (is_array($value))
$toApiData[$key] = ArrayToAPI($value);
else
$toApiData[$key] = Encrypt($value);
}
return $toApiData;
}
这就是我在 API 方面的情况:
ob_start();
var_dump($_POST);
$result = ob_get_clean();
$api->EndRequest(false, array("errorDesc" => "a - " . $result));
function EndRequest(bool $task_completed, array $data = array())
{
$jsonData = array();
$jsonData['success'] = $task_completed;
$jsonData['data'] = $data;
header('Content-type: application/json; charset=utf-8');
echo json_encode($jsonData, JSON_FORCE_OBJECT);
exit;
}
这总是 returns
array(2) { ["uData"]=> string(5) "Array" ["uPost"]=> string(5) "Array" }
我希望我现在更清楚了,谢谢。
问题在于从您的代码发出请求是因为这一行:
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
CURLOPT_POSTFIELDS
不支持多级数组。您的数组值(键所指向的)被转换为字符串,最终为 Array
。使用:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
.. 而不是 "properly" 在 post 请求中序列化多维数组("properly" 因为有很多方法可以做到这一点,但它符合预期 PHP 格式 - []
表示数组)。