在PHP中添加到多维数组

时间:2022-04-01 01:47:19

I have an array being returned from the database that looks like so:

我有一个从数据库返回的数组,如下所示:

$data = array(201 => array('description' => blah, 'hours' => 0),
              222 => array('description' => feh, 'hours' => 0);

In the next bit of code, I'm using a foreach and checking the for the key in another table. If the next query returns data, I want to update the 'hours' value in that key's array with a new hours value:

在下一段代码中,我使用foreach并检查另一个表中的键。如果下一个查询返回数据,我想用新的小时值更新该键的数组中的'hours'值:

foreach ($data as $row => $value){
   $query = $db->query('SELECT * FROM t WHERE id=$row');
   if ($result){
      $value['hours'] = $result['hours'];
   }

It's all fine except that I've tried just about every combination of declarations for the foreach loop, but I keep getting the error that the $value['hours'] is an invalid reference. I've tried declaring $value[] ... but that doesn't work either. I don't need to iterate through $value so another foreach loop isn't necessary.

这很好,除了我已经尝试了foreach循环的每个声明组合,但我不断得到$ value ['hours']是无效引用的错误。我已经尝试声明$ value [] ......但这也不起作用。我不需要遍历$ value,因此不需要另一个foreach循环。

Surely this is easier than my brain is perceiving it.

当然,这比我的大脑感知更容易。

Here's the whole snippet:

这是整个片段:

foreach($_gspec as $key => $value){

            $sql = sprintf('SELECT * FROM List WHERE specialtyID=%s', $key);
            $query = $db->query($sql);

            if ($query->num_rows() !== 0){

                $result = $query->row_array();
                $value['hours'] = $result['hours'];

            }
        }

3 个解决方案

#1


6  

You want

$data[$row]['hours'] = $result['hours']

$ data [$ row] ['hours'] = $ result ['hours']

$row would be better named as $key (that is what it is!)

$ row会更好地命名为$ key(就是这样!)

Some people would suggest using pointers, but I find this way makes more sense to me.

有些人建议使用指针,但我发现这种方式对我来说更有意义。

#2


3  

You need to use ampersand in front of the $value in foreach to pass it by reference like this:

你需要在foreach中的$ value前面使用&符号来通过引用传递它,如下所示:

foreach ($data as $row => &$value){
   $query = $db->query($sql);
   if ($result){
      $value['hours'] = $result['hours'];
   }
}

More info here: http://php.net/manual/en/control-structures.foreach.php

更多信息:http://php.net/manual/en/control-structures.foreach.php

As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.

从PHP 5开始,您可以通过在$前加上$来轻松修改数组的元素。这将分配引用而不是复制值。

#3


0  

Use reference ->

使用参考 - >

foreach ($data as $row => & $value) {
   $query = $db->query('SELECT * FROM t WHERE id=$row');
   // [...]
   if ($result) {
      $value['hours'] = $result['hours'];
   }
}

#1


6  

You want

$data[$row]['hours'] = $result['hours']

$ data [$ row] ['hours'] = $ result ['hours']

$row would be better named as $key (that is what it is!)

$ row会更好地命名为$ key(就是这样!)

Some people would suggest using pointers, but I find this way makes more sense to me.

有些人建议使用指针,但我发现这种方式对我来说更有意义。

#2


3  

You need to use ampersand in front of the $value in foreach to pass it by reference like this:

你需要在foreach中的$ value前面使用&符号来通过引用传递它,如下所示:

foreach ($data as $row => &$value){
   $query = $db->query($sql);
   if ($result){
      $value['hours'] = $result['hours'];
   }
}

More info here: http://php.net/manual/en/control-structures.foreach.php

更多信息:http://php.net/manual/en/control-structures.foreach.php

As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.

从PHP 5开始,您可以通过在$前加上$来轻松修改数组的元素。这将分配引用而不是复制值。

#3


0  

Use reference ->

使用参考 - >

foreach ($data as $row => & $value) {
   $query = $db->query('SELECT * FROM t WHERE id=$row');
   // [...]
   if ($result) {
      $value['hours'] = $result['hours'];
   }
}