我有以下PHP代码为每个帖子设置parentId.每个数据的parentId都成为最后一个帖子ID.我的逻辑出了什么问题?
顺便说一句,如果我把它改成数组,那么每一件事都可以.请帮忙!
$data = array(
(object)array('name' => 'myname')
);
$posts = array(
(object)array('ID' => 1, 'data'=>$data),
(object)array('ID' => 2, 'data'=>$data),
(object)array('ID' => 3, 'data'=>$data)
);
foreach($posts as &$post){
$post->data[0]->parentId = $post->ID;
}
print '<pre>';print_r($posts);die;
die;
Run Code Online (Sandbox Code Playgroud)
结果:
Array
(
[0] => stdClass Object
(
[ID] => 1
[data] => Array
(
[0] => stdClass Object
(
[name] => myname
[parentId] => 3 // expect to be 1
)
)
)
[1] => stdClass Object
(
[ID] => 2
[data] => Array …Run Code Online (Sandbox Code Playgroud) 我有胎儿错误消息说:
致命错误:无法在第3行的C:\ wamp\www\pets_new\lib\database.php中重新声明类数据库
require_once("lib/message.php");
require_once("lib/user.php");
Run Code Online (Sandbox Code Playgroud)
并且都连接到数据库类
班级留言
<?php
require('database.php');
class Message{
Run Code Online (Sandbox Code Playgroud)
班级用户:
<?php
require('database.php');
class User{
Run Code Online (Sandbox Code Playgroud) 我想让 X 个 goroutineCountValue使用并行性进行更新(numRoutines 与你拥有的 CPU 数量一样多)。
解决方案一:
func count(numRoutines int) (countValue int) {
var mu sync.Mutex
k := func(i int) {
mu.Lock()
defer mu.Unlock()
countValue += 5
}
for i := 0; i < numRoutines; i++ {
go k(i)
}
Run Code Online (Sandbox Code Playgroud)
它变成了数据竞争和返回的countValue = 0.
解决方案2:
func count(numRoutines int) (countValue int) {
k := func(i int, c chan int) {
c <- 5
}
c := make(chan int)
for i := 0; i < numRoutines; i++ { …Run Code Online (Sandbox Code Playgroud) 我试图在PhantomJS之上构建一个非常简单的解决方案,但不知何故它在调用时崩溃了phantom.exit().例如,运行以下代码时:
var page = require('webpage').create();
page.onResourceRequested = function (request) {
console.log('Request ' + request.url);
phantom.exit();
};
page.open('http://www.google.com/');
Run Code Online (Sandbox Code Playgroud)
在Ubuntu(12.04.3 LTS)和CentOS(6.4)上发生了分段故障.如果我注释掉phantom.exit()它列出了请求的URL,那么它基本上按预期工作.然而,这phantom.exit()是我真正需要的东西,因为我想在某些情况下终止我的脚本(例如,尝试访问特定域).我觉得我的问题太明显了,但不知道发生了什么.
好的:
我发现这是一个骗局问题
对于我对这个问题的回答,我测试了如果我传递一个浮点数会发生什么printf,并将其打印为十六进制值.
我使用的代码就是这样:
int main()
{
float i = 12.2;
printf("%x == %f\n", i, i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果有点令人惊讶,至少可以说:
60000000 == 26815622268991053043690768862555225929794561970930945383754133458631904088629457662404735694345936594619420127195201411004744101710347755649498829446709248.000000
Run Code Online (Sandbox Code Playgroud)
现在这不太可能,我想.如果我将格式字符串的顺序切换为"%f == %x\n",则float显示为well(12.200000).
现在这引起了我的兴趣.所以我做了一些实验
printf("%f == %x == %f\n", i, i, i);
Run Code Online (Sandbox Code Playgroud)
向我透露,%f格式在%x使用后失败了.所以我尝试过:
printf("%f == %x == %f\n", 12.2, i, 12.2);
Run Code Online (Sandbox Code Playgroud)
哪个让我更加抓我的面条:
12.200000 == 60000000 == 190359837254612272720121546180914982603183590679420528733621571728996711854558612668142684377801318191569131534073063745617380878171517108433746379972393615159806611654135195336789983232.000000
Run Code Online (Sandbox Code Playgroud)
而且,在你问之前:
printf("%f == %x == %f\n", 12.2f, i, 12.2f);
Run Code Online (Sandbox Code Playgroud)
收益率:
12.200000 == 60000000 == …Run Code Online (Sandbox Code Playgroud) 我正在寻找的是这个代码的Javascript等价物:
$map[$card][$bit] = $val;
Run Code Online (Sandbox Code Playgroud)
(如你所知,这也会立即实例化数组)
我尝试和研究了一下,但似乎有很多其他方法可以做到这一点,我想知道什么是我的具体代码的最佳方法.我正在从数据库中检索$ card和$ bit,并创建某种映射,为映射中的每个位置赋值.
Javascript代码:
var jPunten = JSON.parse(data);
var puntmap = [][];
for (var i=0;i<jPunten.length;i++)
{
puntmap[jPunten[i].CARDNR][jPunten[i].BITNR] = jPunten[i].STATDEV;
}
Run Code Online (Sandbox Code Playgroud) 当我做这样的事情时,我正在运行PHP7.0.9:
$s = json_decode('{"1": "xxx"}');//decode json to stdClass
$a = (array)$s;//cast to array
die(var_dump($a, $a['1'], $a[1], count($a)));
Run Code Online (Sandbox Code Playgroud)
我得到这个结果:
array (size=1)
'1' => string 'xxx' (length=3) //<-- key 1 exists
null // $a['1'] yields null
null // $a[1] doesn't work either
int 1 // count shows there is 1 element in the array
Run Code Online (Sandbox Code Playgroud)
我期待这个结果:
array (size=1)
'1' => string 'xxx' (length=3)
string 'xxx' (length=3) // $a['1'] should work
null
int 1
Run Code Online (Sandbox Code Playgroud)
我的问题:为什么我不能访问$a['1'],即使两个count和一个var_dump数组告诉我这个密钥存在?这是PHP中的错误,还是某种功能?
我希望只有在单击正确的按钮时才调用指定的函数.
function callback(func){
func();
}
Run Code Online (Sandbox Code Playgroud)
主要功能及其结果:
尝试A:
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('num1').addEventListener('click', callback(function1));
document.getElementById('num2').addEventListener('click', callback(function2));
});
Run Code Online (Sandbox Code Playgroud)
结果:打开页面并单击任何按钮将运行function1
尝试B:
document.addEventListener('click', function () {
document.getElementById('num1').addEventListener('click', callback(function1));
document.getElementById('num2').addEventListener('click', callback(function2));
});
Run Code Online (Sandbox Code Playgroud)
结果:单击页面上的任意位置将运行function1
尝试C:
document.getElementById('num1').addEventListener('click', callback(function1));
document.getElementById('num2').addEventListener('click', callback(function2));
Run Code Online (Sandbox Code Playgroud)
结果:打开页面并单击按钮将运行function1并重新加载页面
尝试D:
document.addEventListener('click', function () {
//The first ID doesn't exist
document.getElementById('imaginaryNum1').addEventListener('click', callback(function1));
document.getElementById('num1').addEventListener('click', callback(function1));
document.getElementById('num2').addEventListener('click', callback(function2));
});
Run Code Online (Sandbox Code Playgroud)
结果:单击页面上的任意位置将运行function1
尝试E:
//The first ID doesn't exist
document.getElementById('imaginaryNum1').addEventListener('click', callback(function1));
document.getElementById('num1').addEventListener('click', callback(function1));
document.getElementById('num2').addEventListener('click', callback(function2));
Run Code Online (Sandbox Code Playgroud)
结果:打开页面并单击按钮将运行function1并重新加载页面
<?php
$string = '395095427174400_558374047513203';
if(preg_match("/^[0-9][_][0-9]$/",$string)){
echo "True";
}else{
echo "False";
}
?>
Run Code Online (Sandbox Code Playgroud)
为什么我的正则表达式在数字之间不匹配并下划线?
在JavaScript中,为什么以下两个都返回true?
> var a;
undefined
> "a" in window;
true
> a in window;
true
Run Code Online (Sandbox Code Playgroud)
是否存在某种类型强制,或者JavaScript是否存储a为字符串和窗口中的变量?
请随意重写这个问题的标题 - 我不确定如何描述这种令人困惑的现象.
我编写了一个脚本来列出包含特定文件的仓库中的提交.它工作得很好,但我不明白为什么我要写这个:
for c in $(git rev-list "$rev_list"); do
git ls-tree --name-only -r "$c" | grep -q "$file"
if [ $? -eq 0 ]; then
echo "Saw $file in $c"
fi
done
Run Code Online (Sandbox Code Playgroud)
而我通常写这样的事情:
[[ $(git ls-tree --name-only -r "$c" | grep -q "$file") ]] && echo "Saw $file in $c"
# or
[[ ! $(git ls-tree --name-only -r "$c" | grep -q "$file") ]] || echo "Saw $file in $c"
Run Code Online (Sandbox Code Playgroud)
两个短版本都不起作用:它们不输出任何内容.当我写它以显示所有不包含该文件的提交时,我得到输出:
[[ $(git ls-tree --name-only -r "$c" | grep -q "$file") …Run Code Online (Sandbox Code Playgroud) 我有号码 5678
我想要显示如下编号
5678
678
78
8
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
我这样做了
int n = 5678;
for(int i=n; i >= 1; --i)
{
for(int j=1; j <= i; ++j)
{
print("%d",j);
}
print("\n")
}
Run Code Online (Sandbox Code Playgroud) 我有可以通过代码显示的数组:
foreach($form->data as $key => $value){
echo 'Key is '.$key.' and Value is '.$value.'<br />';
}
Run Code Online (Sandbox Code Playgroud)
我得到以下显示:
Key is language and Value is lv-LV
Key is Itemid and Value is 114
Key is option and Value is com_content
Key is pumpis_1 and Value is 1
Key is lietussargs and Value is 2
Run Code Online (Sandbox Code Playgroud)
但我只需要显示[Itemid]其中的值,在这种情况下114
我该怎么做?谢谢!Raivis