I have an array of around 200 records and it looks like the following:
[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
)
I want to convert this array to the following format. The reason is that sid, sname and sdate combination could have multiple values of 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
)
)
)
What would be the best way to get the required structure of the array?
I would say loop through the array, then use var_export
to get the output:
$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).'; ?>');
$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;
}
I presume you get those array result from a join query. I think it rather easier, if you get those datas separate from the database. So your question is similar to this thread: enter link description here
Anyway, you can try this:
$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];
}
}
}
);