CakePHP3に限らないのだと思いますが、オブジェクトの値の取得方法を教えてください。
Usersテーブルを作成し、「cake bake all users」で処理の一式を生成しました。
そのうえで、Usersの index(一覧)ページを表示しています。
その index.ctpファイルの中でレコードの各項目を取得する処理としては、下記のとおり foreach文を使用し、各項目の値を取得しています。
php
1<?php foreach ($users as $user): ?> 2<tr> 3 <td><?= $this->Number->format($user->id) ?></td> 4 <td><?= h($user->username) ?></td> 5 <td><?= h($user->password) ?></td> 6 <td><?= h($user->created) ?></td> 7 <td><?= h($user->modified) ?></td> 8 <td class="actions"> 9 <?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?> 10 <?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?> 11 <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete # {0}?', $user->id)]) ?> 12 </td> 13</tr> 14<?php endforeach; ?>
この処理は理解できるのですが、foreach文を使わず、例えば、1件目のレコードの usernameだけ取得する場合は、どのようにすればいいのでしょうか?
php
1echo $user->0->username;
ではエラーになるのですが、お教えいただけませんでしょうか?

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/23 05:54 編集
2017/02/23 05:59 編集
2017/02/23 06:21