我有以下带有测试数据的数组:
'team' =>
array (
0 =>
array (
'firstName' => 'adfadsf',
'lastName' => 'asdfadsfa',
'twitter' => 'ddasfasdf',
),
1 =>
array (
'firstName' => 'adf',
'lastName' => 'asd',
'twitter' => 'www.twitter.com/dfs',
),
2 =>
array (
'firstName' => 'asd',
'lastName' => 'adf',
'twitter' => 'www.twitter.com/adf',
),
3 =>
array (
'firstName' => 'test',
'lastName' => 'test',
'twitter' => 'www.twitter.com/test',
),
),
我想访问属性firstName
和lastName
。
我尝试了以下内容,其中$ request将team
数组作为一个属性:
foreach ($request as $key => $value) {
Log::info($key);
$team = new Team();
$team->firstname = $request->team[$key]['firstName'];
$team->lastname = $request->team[$key]['lastName'];
$team->twitter = $request->team[$key]['twitter'];
$team->revisions_id = $revision->id;
Log::info("team");
Log::info($team);
$team->save();
}
有关如何访问两个属性firstName
和lastName
的任何建议吗?
感谢您的回复!
答案 0 :(得分:2)
由于$request
中的内容并不完全清楚,但我建议:
foreach ($request as $key => $value) {
Log::info($key);
if ($key == 'team') {
foreach ($value as $ar_team) {
$team = new Team();
$team->firstname = $ar_team['firstName'];
$team->lastname = $ar_team['lastName'];
$team->twitter = $ar_team['twitter'];
$team->revisions_id = $revision->id;
Log::info("team");
Log::info($team);
$team->save();
}
}
}
或者简单地说(如果$request
中foreach
中的其他项目 <}>
foreach ($request['team'] as $ar_team) {
$team = new Team();
$team->firstname = $ar_team['firstName'];
$team->lastname = $ar_team['lastName'];
$team->twitter = $ar_team['twitter'];
$team->revisions_id = $revision->id;
Log::info("team");
Log::info($team);
$team->save();
}
答案 1 :(得分:1)
查看您的数据样本 你应该遍历$ request ['team']并访问$ value以获取firstName和lastname ....
foreach ($request['team'] as $key => $value) {
Log::info($key);
$team = new Team();
$team->firstname = $value['firstName'];
$team->lastname = $value['lastName'];
$team->twitter = $value['twitter'];
$team->revisions_id = $revision->id;
Log::info("team");
Log::info($team);
$team->save();
}