我有一个包含以下元素的数组: $arr = ['N','P','K','Ca','Mg','S', 'Zn', 'B','Cu','Fe','Mn','Mo'];
如何使用数组元素作为对象键以便仅获取特定键的值:
object(stdClass)[727]
public 'id' => int 1
public 'name' => string 'John Doe' (length=8)
public 'fname' => string 'HOUSE' (length=5)
public 'bname' => string 'BLOCK 9' (length=7)
public 'hectares' => float 7.2
public 'cname' => string 'Vitis vinifera' (length=14)
public 'delivery' => string '2210.0000' (length=9)
public 'view_application' => int 1
public 'view_schedule' => int 1
public 'edit_schedule' => int 0
public 'updated_at' => string '2020-07-17 20:03:51' (length=19)
public 'N' => string '4.9000' (length=6)
public 'P' => string '1.1000' (length=6)
public 'K' => string '6.0000' (length=6)
public 'Ca' => string '5.4000' (length=6)
public 'Mg' => string '0.8000' (length=6)
public 'S' => string '0.0000' (length=6)
public 'Zn' => string '0.0210' (length=6)
public 'B' => string '0.0071' (length=6)
public 'Cu' => string '0.0280' (length=6)
public 'Fe' => string '0.0420' (length=6)
public 'Mn' => string '0.0220' (length=6)
public 'Mo' => string '0.0000' (length=6)
我尝试使用这个:
@for ($i = 0; $i < count($arr); $i++)
$object->$arr[$i];
@endfor
但这给出了一个错误Array to string conversion
另一种方法是使用将对象转换为数组$newarr = (array) $object; 然后使用迭代$object[''.arr[$i].''];
但这对我来说不起作用,因为我已经使用该对象完成了大部分项目。有没有办法使用数组元素作为对象键而不将对象转换为数组?
FFIVE