1

我有大约 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
                )
        )
)

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

4

3 回答 3

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).'; ?>');
于 2012-06-26T21:10:14.090 回答
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;
}
于 2012-06-26T21:19:39.730 回答
0

我假设您从连接查询中获得了这些数组结果。如果您将这些数据与数据库分开,我认为这会更容易。所以你的问题类似于这个线程:在此处输入链接描述

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

$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];
            }
        }
    }
);
于 2012-06-26T22:39:28.077 回答