将数组转换为更具可读性和逻辑性的结构

时间:2012-06-26 21:03:05

标签: php arrays

我有一个大约200条记录的数组,它看起来如下:

 [0] => Array
        (
            [sid] => 54
            [sname] => vISL User book
            [sdate] => 2012-03-01 00:00:00
            [pid] => 262
            [cid] => 95
            [at] => 
            [amount] => 0
            [psdate] => 2012-03-14 00:00:00
            [first_name] => Alan
            [last_name] => Darshan
            [pcid] => 95
            [cname] => Invisal
        )
 [1] => Array
        (
            [sid] => 54
            [sname] => vISL User book
            [sdate] => 2012-03-01 00:00:00
            [pid] => 263
            [cid] => 94
            [at] => 
            [amount] => 0
            [psdate] => 2012-03-14 00:00:00
            [first_name] => umed
            [last_name] => sarkar
            [pcid] => 92
            [cname] => Tisal
        )

我想将此数组转换为以下格式。原因是sid,sname和sdate组合可能有多个pid值,cid ... cname:

[0] => Array
(
    [sid] => 54
    [sname] => vISL User book
    [sdate] => 2012-03-01 00:00:00
    [parr] => Array
            (
                [0] => Array 
                    (
                        [pid] => 262
                        [cid] => 95
                        [at] =>
                        [amount] => 0
                        [psdate] => 2012-03-14 00:00:00
                        [first_name] => Alan
                        [last_name] => Darshan
                        [pcid] => 95
                        [cname] => Invisal
                    )
                [1] => Array
                    (
                            [pid] => 262
                            [cid] => 95
                            [at] =>
                            [amount] => 0
                            [psdate] => 2012-03-14 00:00:00
                            [first_name] => Alan
                            [last_name] => Darshan
                            [pcid] => 95
                            [cname] => Invisal
                    )                       
            )
)
[1] => Array
(
        [sid] => 53
        [sname] => tebby book
        [sdate] => 2012-04-01 00:00:00
        [parr] => Array
        (
                [0] => Array
                (
                        [pid] => 162
                        [cid] => 15
                        [at] =>
                        [amount] => 1
                        [psdate] => 2012-03-14 00:00:00
                        [first_name] => edbey
                        [last_name] => balh
                        [pcid] => 93
                        [cname] => Ansasa
                )
                [1] => Array
                (
                        [pid] => 212
                        [cid] => 92
                        [at] =>
                        [amount] => 0
                        [psdate] => 2012-03-14 00:00:00
                        [first_name] => xyz
                        [last_name] => def
                        [pcid] => 91
                        [cname] => vsall
                )
        )
)

获得所需数组结构的最佳方法是什么?

3 个答案:

答案 0 :(得分:1)

我会说循环遍历数组,然后使用var_export来获取输出:

$tmp = Array();
$out = Array();
foreach($input as $row) {
    $tmp[$row['sid']][] = $row;
}
foreach($tmp as $key=>$rows) {
    $tmp[$key] = Array(
        "sid"  =>$rows[0]['sid'],
        "sname"=>$rows[0]['sname'],
        "sdate"=>$rows[0]['sdate'],
        "parr" =>Array()
    );
    foreach($rows as $row) {
        $tmp[$key]['parr'][] = Array(
            "pid"       =>$row['pid'],
            "cid"       =>$row['cid'],
            "at"        =>$row['at'],
            "amount"    =>$row['amount'],
            "psdate"    =>$row['psdate'],
            "first_name"=>$row['first_name'],
            "last_name" =>$row['last_name'],
            "pcid"      =>$row['pcid'],
            "cname"     =>$row['cname']
        );
    }
}
$out = array_values($tmp);
file_put_contents("tmp.php",'<?php $data = '.var_export($out,true).'; ?>');

答案 1 :(得分:0)

$old_data = /* same as in exemple */

$sids = array();

foreach($old_data as $row)
{
    if(!isset($sids[$row['sid']]))
    {
        $sids[$row['sid']] = array();
        foreach(array('sid', 'sname', 'sdate') as $keys)
        {
            $sids[$row['sid']][$keys] = $row[$keys];
        }
        $sids[$row['sid']]['parr'] = array();
    }

    $current_parr = array();
    foreach(array('pid', 'cid', 'at', 'amount', 'psdate', 'first_name', 'last_name', 'pcid', 'cname') as $keys)
    {
        $current_parr[$keys] = $row[$keys];
    }

    $sids[$row['sid']]['parr'][] = $current_parr;
}

答案 2 :(得分:0)

我假设您从连接查询中获取这些数组结果。 如果您将这些数据与数据库分开,我认为这相当容易。 所以你的问题类似于这个主题:enter link description here

无论如何,你可以试试这个:

$pk = 'sid';                // record pk
$pkKeys = array(
    'pid' => array(         // subRecord pk
        'parr' => array(    // subRecords array key => array(subRecord names)
            'pid',
            'cid',
            'amount',
            'psdate',
            'pcid',
        ),
    ),
);

$resultArr = array();
array_walk($records, 
    function ($val, $key) use (&$resultArr, $pkKeys, $pk) {
        $mapped = array();
        foreach ($val as $itemKey => $item) {
            if (in_array($itemKey, array_keys($pkKeys))) {
                $populateData = $pkKeys[$itemKey];
                $subRecordPk = key($populateData);
                foreach($populateData[$subRecordPk] as $populateKey) {
                    if ($subRecordPk !== $populateKey) {
                        $resultArr[$val[$pk]][$subRecordPk][$val[$itemKey]][$populateKey] 
                            = $val[$populateKey];
                        $mapped[$populateKey] = $populateKey;
                    }
                }
            } else if (!in_array($itemKey, $mapped)){
                $resultArr[$val[$pk]][$itemKey] = $val[$itemKey];
            }
        }
    }
);