更改多维数组php的值 [英] Changing values of multidimensional array php
问题描述
这是我第一次在 php 中使用多维数组.我需要更改每个子数组中的第二个数字.
Its my first time working with multidimensional arrays in php. I need to change the second number in each sub array.
我想要的是检查数组中的 Id 是否与数据库中的 Id 匹配.当两者匹配时,我想通过向其添加一个数字来更改子数组中的第二个条目.如果查询中的 Id 与列表中的任何内容都不匹配,我希望将一个新的子数组推送到具有 Id 和 points_description 值的数组末尾.
What I want is to check if the Id in the array matches the Id from the database. When the two match I want to change the 2nd entry in the sub array by adding a number to it. If the Id from the query does not match anything in the list I want a new sub array to be pushed to the end of the array with the values of Id and points_description.
此外,如果有帮助,我的程序现在确实会找到匹配项.唯一的问题是,它不会更新二维数组.
Also, if its helpful, my program right now does find the matches. The only thing is, it does not update the 2D array.
$array = array(array());
while ($row_description = mysqli_fetch_array($query_description)) {
$check = 1;
$is_match = 0;
foreach ($array as $i) {
foreach ($i as $value) {
if ($check == 1) {
if ($row_description['Id'] == $value) {
//$array[$i] += $points_description;
$is_match = 1;
}
}
$check++;
$check %= 2; //toggle between check and points
}
}
if ($is_match == 0) {
array_push($array, array($row_description['Id'], $points_description));
}
}
我觉得我这样做太错误了.我只想遍历我的二维数组并每隔一个值更改一次.预期的输出应该是所有 Id 及其对应点值的打印我希望这足够有帮助.
I feel like Im doing this so wrong. I just want to go through my 2D array and change every second value. The expected output should be a print out of all the Ids and their corresponding point value I hope this is helpful enough.
示例:$row_description['Id'] = 2 和 $array = array(array(2,1), array(5,1), array(6,1))
Example: $row_description['Id'] = 2 and $array = array(array(2,1), array(5,1) , array(6,1))
输出应该是 $array = array(array(2,4), array(5,1), array(6,1))
output should be $array = array(array(2,4), array(5,1) , array(6,1))
如果 $row_description['Id'] = 3 且 $array = array(array(2,1), array(5,1), array(6,1))
if $row_description['Id'] = 3 and $array = array(array(2,1), array(5,1) , array(6,1))
输出应该是 $array = array(array(2,4), array(5,1) , array(6,1),array(3,3))
output should be $array = array(array(2,4), array(5,1) , array(6,1),array(3,3))
推荐答案
默认情况下,当您在 foreach 中使用数组时,PHP 将复制一个数组.为了防止 PHP 创建此副本,您需要使用 &
By default PHP will copy an array when you use it in a foreach.
To prevent PHP from creating this copy you need to use to reference the value with &
简单例子:
<?php
$arrFoo = [1, 2, 3, 4, 5,];
$arrBar = [3, 6, 9,];
默认 PHP 行为:复制
foreach($arrFoo as $value_foo) {
foreach($arrBar as $value_bar) {
$value_foo *= $value_bar;
}
}
var_dump($arrFoo);
/* Output :
array(5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
}
*/
ByReference : 不要创建副本 :
foreach($arrFoo as &$value_foo) {
foreach($arrBar as $value_bar) {
$value_foo *= $value_bar;
}
}
var_dump($arrFoo);
/* Output :
array(5) {
[0]=>
int(162)
[1]=>
int(324)
[2]=>
int(486)
[3]=>
int(648)
[4]=>
&int(810)
}
*/
这篇关于更改多维数组php的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!