在 PHP 中动态访问对象属性数组元素 [英] Dynamically access an object property array element in PHP
本文介绍了在 PHP 中动态访问对象属性数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象,我想与它动态交互.我想将 game1_team1 重命名为:
I have an object, that I would like to interact with dynamically. I would like to rename the game1_team1 in:
$default_value = $individual_match->field_match_game1_team1[0]['value'];
成为 game1_team2、game2_team1、game2_team2、game3_team1 等.基于他们所处的循环.
to be game1_team2, game2_team1, game2_team2, game3_team1, etc. Based on the loop they are in.
我试过了:
$dynamic = 'field_match_game'.$i.'_team'.$j;
$default_value = $individual_match->$dynamic[0]['value'];
但它返回
致命错误:不能使用字符串偏移作为数组
Fatal error: Cannot use string offset as an array
更新:根据 Saul 的回答,我将代码修改为:
Update: Based on Saul's answer, I modified the code to:
$default_value = $individual_match->{'field_match_game'.$i.'_team'.$j}[0]['value'];
消除了致命错误,但没有返回值.
which got rid of the Fatal error, but doesn't return a value.
推荐答案
$individual_match->field_match_game1team1[0]['value'] = 'hello1';
$i = 1;
$j = 1;
$default_value = $individual_match->{'field_match_game'.$i.'team'.$j}[0]['value'];
这篇关于在 PHP 中动态访问对象属性数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文