在我工作的许多项目中,我发现我有嵌套for循环(我将专注于PHP实现,但也适用于javascript)采用这种通用形式:
$array1 = array(........);
$count1 = count($array1);
$invalidState = false;//An invalid state will be looked for in the innermost loop, and if found, break all the loops
for($i = 0; $i < $count1; $i++) {
//parsing, then another loop
$array2 = explode("needle", $array1[$i]);
$count2 = count($array2);
for($j = 0; $j < $count2; $j++) {
//parsing, then sometimes even another another loop
$array3 = explode("different-needle", $array2[$j]);
$count3 = count($array3);
for($k = 0; $k < $count3; $k++) {
//check for …Run Code Online (Sandbox Code Playgroud) 对于我的一个应用程序,我假设比较2个字符串的第一个字符比比较整个字符串的相等性要快.例如,如果我知道只有2个可能的字符串(在一组n个字符串中)可以以相同的字母开头(假设是'q'),如果是这样,它们是相同的字符串,那么我可能会写一个比较如此如下:
if ($stringOne[0] === $stringTwo[0]) $qString = true;
Run Code Online (Sandbox Code Playgroud)
代替:
if ($stringOne === $stringTwo) $qString = true;
Run Code Online (Sandbox Code Playgroud)
但我最近写了一些基准脚本,看起来我错了.也就是说,第二次比较看起来平均比第二次快2-4倍.我的基准看起来像这样:
$x = 'A really really looooooooooooong string';
$y = 'A really really looooooooooooong string';
$timeArray = array();
//Method 1, two-four times faster than Method 2
for($i = 0; $i < 100; $i++) {
$t1 = microtime(true);
for($j = 0; $j < 100000; $j++) {
if ($x === $y) continue;
}
$t2 = microtime(true);
$timeArray[] = $t2 - $t1;
}
echo array_sum($timeArray) …Run Code Online (Sandbox Code Playgroud) 我有这个json编码的字符串
{"allresponses":"{\"id\":\"123456\",\"recipients\":1}"}
Run Code Online (Sandbox Code Playgroud)
我只需要获取id并将其传递给php变量.
这就是我正在尝试的:假设我在变量返回中有该字符串,所以:
$return = '{"allresponses":"{\"id\":\"123456\",\"recipients\":1}"}';
$getid = json_decode($return,true);
echo $getid[0]['id'];
Run Code Online (Sandbox Code Playgroud)
这不起作用; 我得到了致命的错误.你能告诉我为什么吗?怎么了?
在Firefox和Chrome中,我的php($ _GET)正在接收数字和字母以及特殊字符(例如" - "和"("),但字符除外+.这是我的ajax请求:
function ajaxFunction(param) {
var ajaxRequest;
try {
ajaxRequest = new XMLHttpRequest();
} catch (e1) {
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e2) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e3) {
alert("Something is wrong here. Please try again!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function () {
if (ajaxRequest.readyState === 4) document.getElementById("myDiv").innerHTML = ajaxRequest.responseText;
};
ajaxRequest.open("GET", "AJAX_file.php?param=" + param, true);
ajaxRequest.send(null);
}
Run Code Online (Sandbox Code Playgroud)
当用户单击按钮调用ajaxFunction()时,用户首先在单击按钮之前填写输入类型="text".如上所述,在Firefox和Chrome中,我的php文件正在接收数字和字母以及特殊字符(例如" - ","("和")")(AJAX_file.php;非常简洁的代码版本如下但你得到了要点)并成功回应:
<?php include 'connect.php';
//Lots …Run Code Online (Sandbox Code Playgroud) 我有控制器:
class Comments extends Controller
{
public function GenerateComments($id)
{
$theme = DB::table('New_Themes')
->where('id', $id)
->get();
$Comments = NewTheme_Comment::where('id_theme', $id)->get();
$array = $this->tree($Comments);
function tree($Comments, $parent_id = 0, $level=0, $c=0)
{
global $var;
global $array;
global $m;
foreach($Comments as $Comment)
{
if ($Comment['parent_id'] == $parent_id) {
$m++;
$array[$m][0]=$Comment['id'];
If ($level > $var) {$var++; $array[$m][1]=0;} else {
if ($c < 0) $array[$m][1]=$var-$level+1; else {$c--; $array[$m][1]=0;};
$var=$level;
};
tree($Comments, $Comment['id'], $level+1,$c);
}
};
return $this->$array;
};
return view('comments', ['Themes'=> $theme, 'Comments'=> $Comments, …Run Code Online (Sandbox Code Playgroud)