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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。