更新和排序多维PHP数组

时间:2021-11-03 21:27:03

I have a mySQL query that dumps into an array, is sorted and then displayed, and that works perfectly.

我有一个mySQL查询,转储到一个数组,排序,然后显示,这是完美的。

My problem is that I want to update these values, re-sort them and display. I've been able to update the values, but it's within the same for loop and so it doesn't re-sort. So then I close the loop and re-sort but it doesn't save the updates I made in the first loop. Help is much appreciated.

我的问题是我想更新这些值,重新排序并显示。我已经能够更新这些值,但它在for循环中是相同的,所以它不会重新排序。然后我关闭循环并重新排序,但它不保存我在第一个循环中所做的更新。非常感谢帮助。

foreach ($arr as $mks){
if ($mks['Column1']==$Var) {$mks['Column2'] = $mks['Column2'] + 1;
}

My sorting code

我的排序代码

foreach ($arr as $mks){
    echo $mks['Column1'] . ", " . $mks['Column2'] . "<br>";
}

How do I get this to re-save into my array properly?! I've tried googling this for most of the morning but has lead to frustration.

如何才能将其重新保存到我的阵列中?!我早上大部分时间都试过谷歌搜索,但却导致沮丧。

3 个解决方案

#1


0  

I think this is what you need:

我想这就是你需要的:

foreach ($arr as $key => $mks){
    if ($mks[$key] == $Var) {
        $arr[$key] = $mks+1;
    }
}

Now, $arr will contain the modified array, and you can use / modify it further as you need.

现在,$ arr将包含已修改的数组,您可以根据需要进一步使用/修改它。

#2


0  

I don't see any "sorting code" but you can try to use references "&" instead of copy vars in your foreach(s) :

我没有看到任何“排序代码”,但你可以尝试在你的foreach中使用引用“&”而不是copy vars:

foreach($arr as & $mks){
...
}

Instead of copying your cells it will to give you a reference and every modifications done on it will be saved in the array.

它不会复制您的单元格,而是为您提供参考,并且对其进行的每个修改都将保存在数组中。

#3


0  

Try changing your array like this instead

尝试更改这样的数组

foreach ($arr as $key => $mks){
    if ($mks['Column1']==$Var) {$arr[$key]['Column2'] = $mks['Column2'] + 1;
}

#1


0  

I think this is what you need:

我想这就是你需要的:

foreach ($arr as $key => $mks){
    if ($mks[$key] == $Var) {
        $arr[$key] = $mks+1;
    }
}

Now, $arr will contain the modified array, and you can use / modify it further as you need.

现在,$ arr将包含已修改的数组,您可以根据需要进一步使用/修改它。

#2


0  

I don't see any "sorting code" but you can try to use references "&" instead of copy vars in your foreach(s) :

我没有看到任何“排序代码”,但你可以尝试在你的foreach中使用引用“&”而不是copy vars:

foreach($arr as & $mks){
...
}

Instead of copying your cells it will to give you a reference and every modifications done on it will be saved in the array.

它不会复制您的单元格,而是为您提供参考,并且对其进行的每个修改都将保存在数组中。

#3


0  

Try changing your array like this instead

尝试更改这样的数组

foreach ($arr as $key => $mks){
    if ($mks['Column1']==$Var) {$arr[$key]['Column2'] = $mks['Column2'] + 1;
}