I have a PHP object that I would like to convert to a multidimensional array but I can't seem to work it out.
This is my data structure...
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] =>
)
And I want to convert it to an array pattern like this...
[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]
]
]
At the moment I have scrambled together this, I just can't work it out!
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'])
);
}
vq_refvalues all the same?$obj = new x(); var_dump($obj); var_dump((array)$obj);without any problems makes the object into associative array. You would just need to cast each object that is contained into your primary array.