这是我的数据结构...
Array (
[0] => stdClass Object (
[code] => 63205RMVF
[vq_ref] => 60001689
[start] => 2012-01-10
[done_elm] =>
[unitref] => D5027581
[vqdesc] => Diploma in Dental Nursing
[descrip] => Scientific principles in the management of oral health diseases and dental procedures
[credit_val] => 5
[achieve] =>
)
[1] => stdClass Object (
[code] => 63205RMVF
[vq_ref] => 60001689
[start] => 2012-01-10
[vq_exp] => 2014-01-09
[enddate] =>
[vq_act] =>
[done_elm] =>
[unitref] => D5027600
[vqdesc] => Diploma in Dental Nursing (
QCF
)
[bodynum] => Unit 306
[descrip] => Provide chairside support during the assessment of patients\' oral health
[credit_val] => 1
[start_1] => 2013-03-19
[expect] =>
[achieve] => 2013-11-29
[status] => A
[done_elm_1] => 100
)
[2] => stdClass Object (
[code] => 63205RMVF
[vq_ref] => 60001689
[start] => 2012-01-10
[vq_exp] => 2014-01-09
[enddate] =>
[vq_act] =>
[done_elm] =>
[unitref] => DN317
[vqdesc] => Diploma in Dental Nursing (
QCF
)
[bodynum] => DN317
[descrip] => Level 3 Principles and theory of Dental Nursing
[credit_val] =>
[start_1] =>
[expect] =>
[achieve] => 2013-09-19
[status] => A
[done_elm_1] =>
)
我想将其转换为这样的数组模式...
[aim] = [
[code]
[vq_ref]
[units] => [
[start]
[vq_exp]
[enddate]
[vq_act]
[done_elm]
[unitref]
[vqdesc]
[bodynum]
[descrip]
[credit_val]
[start_1]
[expect]
[achieve]
[status]
[done_elm_1]
]
]
在我把这拼凑在一起的那一刻,我只是无法解决!
foreach($results as $result)
{
$unit['aim'][$result->vq_ref]['unit'] = array()
'unit_ref' => $result->unitref,
'unit_title' => $result->descrip,
'start' => $result->start,
'achieved' => $result->achieve,
'value' => $result->credit_val
);
}
foreach($results as $result)
{
$data['aim'][$result->vq_ref] = array(
'aim_ref' => $result->vq_ref,
'aim_title' => $result->vqdesc,
'units' => array($unit['aim'][$result->vq_ref]['unit'])
);
}
$unit['aim'] = array_map('get_object_vars',$yourArray);
一种方法可以用此功能映射您的数组,并根据需要将属性分为两个集合:
function split_collection($object){
$array = get_object_vars($object);
$newArray = [$array['code'],$array['vqref'],"units"=>[]];
unset($array['code']);
unset($array['vq_ref']);
$newArray['units'] = $array;
return $newArray;
}
$unit['aim'] = array_map('split_collection',$yourCollection);
我实际上是通过使用此解决的。一开始我并不遥远,但这似乎对我来说很好...
foreach($results as $result)
{
$units[$result->vq_ref][] = array(
'unit_ref' => $result->unitref,
'unit_title' => $result->descrip,
'start' => $result->start,
'achieved' => $result->achieve,
'value' => $result->credit_val
);
}
foreach($results as $result)
{
$data['aim'][$result->vq_ref] = array(
'aim_ref' => $result->vq_ref,
'aim_title' => $result->vqdesc,
'units' => $units[$result->vq_ref]
);
}
在问答的自我撤职帖子上,可以简化此分组任务为一个循环。 为了访问数据,无需将每个对象转换为临时数组。
aim_ref
和units
的新孩子。 demo
$data = [];
foreach ($results as $result) {
$data['aim'][$result->vq_ref]['aim_ref'] = $result->vq_ref;
$data['aim'][$result->vq_ref]['aim_title'] = $result->vqdesc;
$data['aim'][$result->vq_ref]['units'][] = [
'unit_ref' => $result->unitref,
'unit_title' => $result->descrip,
'start' => $result->start,
'achieved' => $result->achieve,
'value' => $result->credit_val,
];
}