PHP 一遍又一遍地改变数组

发布于 2024-09-02 12:52:49 字数 474 浏览 2 评论 0原文

我有任何数组

$num_list = array(42=>'0',44=>'0',46=>'0',48=>'0',50=>'0',52=>'0',54=>'0',56=>'0',58=>'0',60=>'0');

,我想在循环时更改特定值,

    while(list($pq, $oin) = mysql_fetch_row($result2)) {
        $num_list[$oin] = $pq;
    }

所以我想将 58 更改为 403,而不是 0。

但是,我总是最终得到最后的更改,而不是之前的更改。所以它最终总是像这样 0,0,0,0,0,0,0,0,0,403 而不是 14,19,0,24,603,249,0,0,0,403

我该怎么做才能不覆盖它?

谢谢

I have any array

$num_list = array(42=>'0',44=>'0',46=>'0',48=>'0',50=>'0',52=>'0',54=>'0',56=>'0',58=>'0',60=>'0');

and I want to change specific values as I go through a loop

    while(list($pq, $oin) = mysql_fetch_row($result2)) {
        $num_list[$oin] = $pq;
    }

So I want to change like 58 to 403 rather then 0.

However I always end up getting just the last change and non of the earlier ones. So it always ends up being something like
0,0,0,0,0,0,0,0,0,403
rather then
14,19,0,24,603,249,0,0,0,403

How can I do this so it doesn't overwrite it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡紫姑娘! 2024-09-09 12:52:49

好吧,您明确编码每个条目都应该替换为数据库中的值(即使是“0”)。
您只能替换非零值的值:

while(list($pq, $oin) = mysql_fetch_row($result2)) {
    if ($pq !== "0") $num_list[$oin] = $pq;
}

Well, you explicititly coded that each entry should be replaced with the values from the database (even with "0").
You could replace the values on non-zero-values only:

while(list($pq, $oin) = mysql_fetch_row($result2)) {
    if ($pq !== "0") $num_list[$oin] = $pq;
}
无所谓啦 2024-09-09 12:52:49

我没有让你更清楚,我以为你只是问这个。检查这个

while(list($pq, $oin) = mysql_fetch_row($result2)) {
       if($oin==58) {
           $num_list[$oin] = $pq;
       }
    }

I don't get you more clear, i thought your asking this only. Check this

while(list($pq, $oin) = mysql_fetch_row($result2)) {
       if($oin==58) {
           $num_list[$oin] = $pq;
       }
    }
终陌 2024-09-09 12:52:49

在我的模拟测试中(尽管您的信息非常匮乏),您的代码运行良好并产生了您想要的结果。检查您放入数组的第二个查询参数 - 即 $pg,这就是您应该得到的 0,0,0,0,0...403 或者其他事情可能是您的 $oin 数字不存在于 $ num_list 键。
我使用 mysqli 驱动程序测试了您的代码,但资源提取 fetch_row 是相同的。

请记住一件事 - 如果您的查询记录数大于 $numlist 数组,并且 $oin 数字不是唯一的,您的 $numlist 可能很容易被以下数据覆盖,而且 $numlist 可能会获得更多额外不需要的元素。

始终尝试提供您的问题的更广泛背景,可能有很多方法可以解决该问题,并且帮助会更快到达。

In my simulated tests (although You are very scarce with information), Your code works well and produces the result that You want. Check the second query parameter, that You put into array - namely $pg, thats what You should get there 0,0,0,0,0...403 OR Other thing might be that Your $oin numbers are not present in $num_list keys.
I tested Your code with mysqli driver though, but resource extraction fetch_row is the same.

Bear in mind one more thing - if Your query record number is bigger than $numlist array, and $oin numbers are not unique, Your $numlist may be easily overwritten by the folowing data, also $numlist may get a lot more additional unwanted elements.

Always try to provide the wider context of Your problem, there could be many ways to solve that and help would arrive sooner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文