简体   繁体   English

如何在PHP中更改多维数组中的值

[英]How to change a value in multidimension array in PHP

I want to change some values in my multidimension array in PHP. 我想在PHP的多维数组中更改一些值。

Suppose that I have: 假设我有:

$photographer[0]['uid'] = '1001';
$photographer[0]['point'] = '0';  
$photographer[1]['uid'] = '1002';
$photographer[1]['point'] = '1';

I want to change point of photographer that have uid = '1001' to 3. How can I do it? 我想将uid ='1001'的摄影师的拍摄点更改为3。我该怎么做?

Rather than the design you have at present, with the array of photographers 0 indexed, why not have the points indexed by uid? 而不是您目前拥有的设计,而是将摄影师0的数组索引了,为什么不用uid索引这些点呢?

$photographer[1001]['point'] = '0';
$photographer[1002]['point'] = '1';

Only by looping through each member of the array: 仅通过遍历数组的每个成员:

for ($i = 0; $i <= count($photographer); $i++)
 {

   if ($photographer[$i]['uid'] == "1001")
     $photographer[$i]['point'] = 3;
 }

I don't know your situation but maybe it might make sense to use the uid as array key, then you could do this: 我不知道您的情况,但是使用uid作为数组键可能有意义,那么您可以这样做:

$photographer[1001]["point"] = 3;
for ($i = 0; i < count($photographer); ++i) {
  if ($photographer[$i]['uid'] == '1001') {
    $photographer[$i]['point'] = 3;
    break;
  }
}

If this content is being loaded from a database into the multidimensional array, and the uid is the primary key, your best bet is probably to do as Pekka suggested and set the array key as the uid. 如果将此内容从数据库加载到多维数组中,并且uid是主键,则最好的选择可能是按照Pekka的建议进行操作,并将数组键设置为uid。 That will allow you to identify individual photographers via your table. 这样一来,您就可以通过表格来识别各个摄影师。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM