概要
閲覧数:999
投稿日:2016-06-27
更新日:2016-06-27
stdClass を foreach
コード
$users = [ '東京' => (object)['user_id' => 'a'], '大阪' => (object)['user_id' => 'b'], ]; $i = 0; foreach ($users as $user) { $user->total = ++$i; } var_dump($users);
結果
array(2) { ["東京"]=> object(stdClass)#1 (2) { ["user_id"]=> string(1) "a" ["total"]=> int(1) } ["大阪"]=> object(stdClass)#2 (2) { ["user_id"]=> string(1) "b" ["total"]=> int(2) } }