【发布时间】:2017-12-21 07:00:43
【问题描述】:
我使用的 API(Oracle 服务云的 ConnectPHP)遵循链接方法。例如:
$incident = new Incident();
$incident->CustomFields->c->make = "Same value";
$incident->StatusWithType->Status->ID = 34;
$incident->save();
如果$incident 对象的后续属性是动态生成的,我将如何实现相同的目标?例如:
$data = array();
$data[0]['parts'] = array('CustomFields', 'c', 'make');
$data[0]['value'] = "Some value";
$data[1]['parts'] = array('StatusWithType', 'Status', 'ID');
$data[1]['value'] = 34;
$incident = new Incident();
foreach($data as $array)
{
foreach($array['parts'] as $key)
{
// how will I generate
// (1) $incident->CustomFields->c->make = $array['value']
// (2) $incident->StatusWithType->Status->ID = $array['value']
}
}
$incident->save();
我尝试了什么
$incident = new Incident();
foreach($data as $array)
{
$parts = implode('->', $array['parts']);
$incident->{$parts} = $array['value']; // this doesn't work even though $parts is coming out with the expected pattern because I think it is converting it into a string representation
}
$incident->save();
【问题讨论】:
-
$incident->CustomFields->c->make 和 $incident->StatusWithType->Status->ID 是静态键,但您想用动态值更新它们吗??
-
键也可能会改变...我只是以其中两个键为例。但是配对是固定的,即
StatusWithType后面总是跟Status->ID。 -
so array('CustomFields', 'c', 'make') 应该是 CustomFields->c->make........ array('StatusWithType', 'Status', 'ID') 应该是StatusWithType->Status->ID........ array('1', '2', '3') => 1->2->3 。 .....像这样......你期待吗??
-
是的,你是对的@kranthi
-
请试试这个 foreach($data as $array) { $incident->{$array['parts'][0]}->{$array['parts'][1]} ->{$array['parts'][2]} = $array['value']; }
标签: php oop rightnow-crm oracle-service-cloud