我每秒都会通过ajax在JavaScript程序中获取此数组,请求的响应大约需要3到4秒才能完成。因此,我希望将此数组拆分为块但未能完成数组
use PHPUnit\Framework\TestCase;
class Mocks extends TestCase{
public function makeMysqliMock(array $queries): mysqli{
// build mocked mysqli-object
#$link = $this
# ->getMockBuilder("mysqli")
# // set methods
# ->setMethods(['__get', "query", "real_escape_string"])
# ->getMock();
$link = $this->createMock("mysqli");
// set method 'real_escape_string'
$link->expects($this->any())
->method("real_escape_string")
->will($this->returnCallback(function($str){
return addslashes($str);
}));
// set method 'query'
$link->expects($this->any())
->method("query")
->will($this->returnCallback(function(string $query) use ($queries){
// pick the query result for the current test
$data = isset($queries[$query]) ? $queries[$query] : null;
// build mocked 'mysqli_result'
if (is_array($data)){
$result = $this
->getMockBuilder("mysqli_result")
->setMethods(["fetch_assoc", "__get"])
->disableOriginalConstructor()
->getMock();
// build simulated fetch method
$result->expects($this->any())
->method("fetch_assoc")
->withAnyParameters()
->will($this->returnCallback(function() use ($data){
// set internal result pointer
static $index = 0;
// return current result - increment pointer
return isset($data[$index]) ? $data[$index++] : false;
}));
$result->expects($this->at(0))
->method("__get")
->with($this->equalTo("mysqli_num_rows"))
->will($this->returnValue(count($data)));
return $result;
}else {
return is_null($data) ? false : 1;
}
}));
return $link;
}
}
请帮助我,我对此感到困惑......
答案 0 :(得分:0)
首先尝试解析:
var json = '{"m":true,"s":true,"q":{"eur":{"b":"1.06783","a":"1.06790","h":"1.06978","l":"1.06729"},"gbp":{"b":"1.24407","a":"1.24416","h":"1.25093","l":"1.24397"},"aud":{"b":"0.76456","a":"0.76464","h":"0.76630","l":"0.76387"},"cad":{"b":"1.33550","a":"1.33557","h":"1.33583","l":"1.33251"},"jpy":{"b":"111.996","a":"112.002","h":"112.204","l":"111.700"},"chf":{"b":"1.00110","a":"1.00117","h":"1.00188","l":"0.99850"},"xau":{"b":"1241.75","a":"1242.1","h":"1244.69","l":"1239.53"},"xag":{"b":"18.056","a":"18.098","h":"18.175","l":"18.02"},"nok":{"b":"8.58853","a":"8.5901","h":"8.59342","l":"8.55128"},"sek":{"b":"8.9308","a":"8.9323","h":"8.9515","l":"8.92151"},"sgd":{"b":"1.3975","a":"1.3977","h":"1.3959","l":"1.3987"},"pkr":{"b":"104.8300","a":"105.8300","h":"104.8300","l":"104.620"},"dkk":{"b":"6.9645","a":"6.9648","h":"6.9500","l":"6.9691"},"nzd":{"b":"0.69835","a":"0.69859","h":"0.70084","l":"0.69794"},"zar":{"b":"13.3688","a":"13.37396","h":"13.6475","l":"13.232"},"hkd":{"b":"7.77017","a":"7.77098","h":"7.77244","l":"7.76847"},"inr":{"b":"64.8500","a":"64.8600","h":"64.925","l":"64.7800"},"try":{"b":"3.63447","a":"3.63558","h":"3.65941","l":"3.63076"},"pln":{"b":"3.9514","a":"3.9520","h":"3.9361","l":"3.9520"},"cnh":{"b":"6.88261","a":"6.88352","h":"6.89029","l":"6.8706"},"myr":{"b":"0.2255","a":"0.2265","h":"0.2253","l":"0.2257"},"sar":{"b":"0.2662","a":"0.2672","h":"0.2662","l":"0.2662"}}}';
json = JSON.parse(json);
console.log(json.q.try);
现在您可以轻松访问所需的字段...
答案 1 :(得分:0)
要将其拆分为数组块,您可以使用$ .map
var json = '{"m":true,"s":true,"q":{"eur":{"b":"1.06783","a":"1.06790","h":"1.06978","l":"1.06729"},"gbp":{"b":"1.24407","a":"1.24416","h":"1.25093","l":"1.24397"},"aud":{"b":"0.76456","a":"0.76464","h":"0.76630","l":"0.76387"},"cad":{"b":"1.33550","a":"1.33557","h":"1.33583","l":"1.33251"},"jpy":{"b":"111.996","a":"112.002","h":"112.204","l":"111.700"},"chf":{"b":"1.00110","a":"1.00117","h":"1.00188","l":"0.99850"},"xau":{"b":"1241.75","a":"1242.1","h":"1244.69","l":"1239.53"},"xag":{"b":"18.056","a":"18.098","h":"18.175","l":"18.02"},"nok":{"b":"8.58853","a":"8.5901","h":"8.59342","l":"8.55128"},"sek":{"b":"8.9308","a":"8.9323","h":"8.9515","l":"8.92151"},"sgd":{"b":"1.3975","a":"1.3977","h":"1.3959","l":"1.3987"},"pkr":{"b":"104.8300","a":"105.8300","h":"104.8300","l":"104.620"},"dkk":{"b":"6.9645","a":"6.9648","h":"6.9500","l":"6.9691"},"nzd":{"b":"0.69835","a":"0.69859","h":"0.70084","l":"0.69794"},"zar":{"b":"13.3688","a":"13.37396","h":"13.6475","l":"13.232"},"hkd":{"b":"7.77017","a":"7.77098","h":"7.77244","l":"7.76847"},"inr":{"b":"64.8500","a":"64.8600","h":"64.925","l":"64.7800"},"try":{"b":"3.63447","a":"3.63558","h":"3.65941","l":"3.63076"},"pln":{"b":"3.9514","a":"3.9520","h":"3.9361","l":"3.9520"},"cnh":{"b":"6.88261","a":"6.88352","h":"6.89029","l":"6.8706"},"myr":{"b":"0.2255","a":"0.2265","h":"0.2253","l":"0.2257"},"sar":{"b":"0.2662","a":"0.2672","h":"0.2662","l":"0.2662"}}}';
json = JSON.parse(json);
var arr = $.map(json.q, function(el) { return el });
console.log(arr);
答案 2 :(得分:0)
使用json_decode()将JSON数据转换为多维数组。 当TRUE作为第二个参数传递时,返回的对象将被转换为关联数组。
print_r(json_decode(json_array, true));
之后使用array_chunk()函数将多维数组拆分成更小的块。
示例:
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));