将数组转换为多维数组

时间:2014-01-03 14:47:08

标签: php arrays multidimensional-array

我有一个PHP对象,我想将其转换为多维数组,但我似乎无法解决它。

这是我的数据结构...

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'])
    );
}

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

您要搜索的功能称为get_object_vars

 $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);

答案 1 :(得分:0)

我实际上是通过使用它解决了它。我在第一时间并不遥远,但这对我来说似乎没问题......

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]
    );
}