doudiao2335 2017-11-21 21:39
浏览 24
已采纳

php foreach无法设置对象的数组属性[重复]

This question already has an answer here:

I have an array of objects $factories and i'm looping with foreach through that array and then through array property of the object.

$f->inputResources[0]['checked']=2; // this sets the array correctly
foreach($factories as $f){
    foreach($f->inputResources as $i){
        echo $i['resource']." ".$i['amount']." ".$i['checked']."<br>"; // these values are correctly retrieved from the object
        $i['checked']=5; // but this doesn't set the object's array although it sets the $i['checked'] that is echoed down correctly

        echo $i['resource']." ".$i['amount']." ".$i['checked']."<br>";//looks like it was set correctly

        echo print_r($f-> inputResources); //here we figure out it wasn't set correctly to 5 and it's still the old value
    }
}

Why does $i['checked']=5; not set the assoc array property of object correctly while echoing $i['checked'] retrieves the correct property value out of the object?

Here is class code:

class factory
{
var $id;
var $name;
var $player; //the owner
var $planet;
var $type;
var $govOwned; //0 player owned 1 government owned
var $output; //0 factory storage 1 market
var $price; //price of auto market ouput
var $resource;//resource which factory is producing

var $workers;
var $wage;
var $money;
var $starveIndex;

var $inputResources;
var $reservedResources;

var $storage;

public function __construct($data)
{
    $this->id = $data['id'];
    $this->name = $data['name'];
    $this->player = $data['player_id'];
    $this->planet = $data['planet_id'];
    $this->type = $data['factory_type'];
    $this->govOwned = $data['gov_owned'];
    $this->output = $data['output_where'];
    $this->price = $data['price'];
    $this->resource = $data['resource_id'];
    $this->workers = $data['workers'];
    $this->wage = $data['worker_wage'];
    $this->starveIndex = $data['workers_starve'];

    $this->inputResources[] = array('resource' => $data['resource_needed'], 'amount' => $data['amount'], 'checked' => 0);

    // etc.
}

public function addInputResource($resource, $amount)
{
    $this->inputResources[] = array('resource' => $resource, 'amount' => $amount, 'checked' => 0);
}

public function addStorage($resource, $amount)
{
    $this->storage[] = array('resource' => $resource, 'amount' => $amount);
}

}

PS: yes I could probably solve this by a setter method... still i'm curious why this isn't working. Also php7

</div>
  • 写回答

1条回答 默认 最新

  • doude4924 2017-11-21 21:48
    关注

    From the PHP foreach manual: http://php.net/manual/en/control-structures.foreach.php

    in order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

    <?php
        $arr = array(1, 2, 3, 4);
        foreach ($arr as &$value) {
           $value = $value * 2;
        }
        // $arr is now array(2, 4, 6, 8)
        unset($value); // break the reference with the last element
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳