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 に答える