php循环并存储到数组中
php for loop and storing in to arrays
我正试图通过ajax请求将一些值从javascript传递到php文件,并使用php将每个结果(来自for循环)存储到数组中。
queryData={"data":{"data_string":{"data":"medicine","default_field":"Content"}}}
testArgument=0;
$.ajax({
url:"test/queryManipulate.php",
type: 'POST',
datatype: 'json',
data: {field : queryData, start : testArgument},
success:function(jsonQuery)
{
alert(jsonQuery);
}
});
<?php
$i=0;
for ($from = 0; $from <= 50; $from+=10)
{
$object=json_decode($_POST["field"]);
$object->from=$from;
$object=json_encode($object);
$ch = curl_init("http://localhost:9200/algotree//_search");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $object);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$test= curl_exec($ch);
curl_close ($ch);
$newArray[$i]= $test;
$i++;
echo $newArray[2];
}
?>
MY for循环不起作用。我使用它的方式正确吗?我如何将每个结果存储到php中的数组中?
第一个循环应该是
<?php
for($from = $_POST["start"]; $from<50;$from+=10)
{
// other code
}
注意$from+=10,而不是$from+10。
在javascript中,在转换为json字符串之前,我们必须使用数组和对象(如)格式化数据
data_string = new Object();
data = new Array();
data_string[data] = "medicine";
data_string[default_field] = "Content";
data.push(data_string);
然后将数据编码为json,结果会像一样
{"data":{"data_string":{"data":"medicine","default_field":"Content"}}}
然后通过ajax传递到php文件,然后使用解码json
php 中的json_encode(data);
然后做进一步的处理。。
相关文章:
- 将数组存储到本地存储中,而不是替换
- 如何将数组存储
- 在 laravel 4 上使用循环将数据数组存储到单个数组中
- 将对象/数组存储在 jQuery Cookie 中
- 想要将数组存储为 JavaScript 映射中的值
- 在 mysql 中使用多维数组存储 $_POST
- 将php多维数组存储在localstorage中
- 将数组存储在jQuery cookie中
- 如何将php数组存储在一个cookie中,然后通过javascript读取它
- 如何用单引号将数组存储在javascript中?有可能吗
- 将数组存储在html数据属性中
- 使用javascript将数组存储为json中的密钥对
- 将数组存储在函数内部与存储在对象内部有什么好处吗
- 试图使用javascript将Object.keys(obj)的返回数组存储在数组中,但得到错误“ReferenceEr
- 在javascript中使用数组存储一组具有固定值的变量
- 如何在localStorage (Javascript)中使用数组存储JSON
- 数组存储javascript
- 我如何从数组存储访问JS函数
- 关于如何使用数组存储其他变量名称的任何想法
- 在chartJs中使用动态数组存储标签和数据