These arrays are the result of Store Procedure. They are to build a Google Chart, therefore I need transform the array to a specific format. I also want to remove the $col
and $row
elements that have a Null value. These arrays are dynamic to their length variable, but a $row
element array always has the same length as a $cols
array. I achieve that by using eval()
to build the if
statement dynamically.
Is there a better way to do this without using eval()
? Are there any improvements that can be done?
I'm using PHP 5.4.
$cols = Array
(
0 => 'Name',
1 => 'MCR',
2 => 'MCD',
3 => 'COM',
4 => 'INV',
5 => 'IDM',
6 => 'PPO',
7 => 'HKD',
8 => 'ALL',
9 => 'POS',
10 => 'MCD-SP',
11 => 'MCD-BH',
12 => 'MCD-CL',
13 => 'MCD-CO',
14 => 'MCR-SP',
15 => 'MCR-PO'
);
$rows = Array(
0 => Array(
0 => 'Primero',
1 => 19683.43,
2 => NULL,
3 => 3757.38,
4 => NULL,
5 => NULL,
6 => NULL,
7 => NULL,
8 => NULL,
9 => NULL,
10 => NULL,
11 => NULL,
12 => NULL,
13 => NULL,
14 => NULL,
15 => NULL
),
1 => Array
(
0 => 'Segundo',
1 => 15340.5767,
2 => NULL,
3 => NULL,
4 => NULL,
5 => NULL,
6 => NULL,
7 => NULL,
8 => NULL,
9 => NULL,
10 => NULL,
11 => NULL,
12 => NULL,
13 => NULL,
14 => NULL,
15 => NULL
),
2 => Array
(
0 => 'Tercero',
1 => NULL,
2 => NULL,
3 => NULL,
4 => NULL,
5 => NULL,
6 => NULL,
7 => NULL,
8 => NULL,
9 => NULL,
10 => 4969.65,
11 => NULL,
12 => NULL,
13 => NULL,
14 => 23695.39,
15 => NULL
),
3 => Array
(
0 => 'Cuarto',
1 => NULL,
2 => NULL,
3 => NULL,
4 => NULL,
5 => NULL,
6 => NULL,
7 => NULL,
8 => NULL,
9 => NULL,
10 => NULL,
11 => 45974.86,
12 => NULL,
13 => NULL,
14 => NULL,
15 => NULL
),
4 => Array
(
0 => 'Quinto',
1 => NULL,
2 => NULL,
3 => NULL,
4 => NULL,
5 => NULL,
6 => NULL,
7 => NULL,
8 => NULL,
9 => NULL,
10 => 1405.8,
11 => 39244,
12 => NULL,
13 => NULL,
14 => NULL,
15 => NULL
)
);
$qty_col = count($cols) - 1;
$qty_rows = count($rows);
//echo $qty_rows;
$ifScript = '';
for ($a = 1; $a <= $qty_col; $a++) {
$unsetScript = 'unset(';
$ifScript .='if(';
for ($b = 0; $b <= $qty_rows; $b++) {
$ifScript .= 'empty($rows[' . $b . '][' . $a . '])';
$unsetScript .='$rows[' . $b . '][' . $a . ']';
if ($b == $qty_rows) {
$ifScript .='){';
//$ifScript .='){<br>';
$unsetScript .=', $cols[' . $a . ']);';
} else {
$ifScript .= ' && ';
$unsetScript .=', ';
}
}
$ifScript .= $unsetScript . '}';
//$ifScript .= $unsetScript . '<br>}<br>';
}
//echo $ifScript;
eval($ifScript);
$cols = array_values($cols);
$rows = array_map('array_values', $rows);
foreach ($cols as $key => $value) {
$cols[$key] = array(
'id' => NULL,
'label' => $value
);
$type = ($key == 0) ? 'string' : 'number';
$cols[$key]['type'] = $type;
}
foreach ($rows as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
$val = (empty($value2)) ? '0.00' : $value2;
$format = ($key2 == 0) ? strtoupper(trim($value2)) : '$' . number_format($value2, '2', '.', ',');
$value1[$key2] = array(
'v' => $val,
'f' => $format
);
}
$rows[$key1] = array(
'c' => $value1
);
}
$newArray = array(
'cols' => $cols,
'rows' => $rows
);
echo '<pre>';
print_r($newArray);
echo '</pre>';
Output
Array
(
[cols] => Array
(
[0] => Array
(
[id] =>
[label] => Name
[type] => string
)
[1] => Array
(
[id] =>
[label] => MCR
[type] => number
)
[2] => Array
(
[id] =>
[label] => COM
[type] => number
)
[3] => Array
(
[id] =>
[label] => MCD-SP
[type] => number
)
[4] => Array
(
[id] =>
[label] => MCD-BH
[type] => number
)
[5] => Array
(
[id] =>
[label] => MCR-SP
[type] => number
)
)
[rows] => Array
(
[0] => Array
(
[c] => Array
(
[0] => Array
(
[v] => Primero
[f] => PRIMERO
)
[1] => Array
(
[v] => 19683.43
[f] => $19,683.43
)
[2] => Array
(
[v] => 3757.38
[f] => $3,757.38
)
[3] => Array
(
[v] => 0.00
[f] => $0.00
)
[4] => Array
(
[v] => 0.00
[f] => $0.00
)
[5] => Array
(
[v] => 0.00
[f] => $0.00
)
)
)
[1] => Array
(
[c] => Array
(
[0] => Array
(
[v] => Segundo
[f] => SEGUNDO
)
[1] => Array
(
[v] => 15340.5767
[f] => $15,340.58
)
[2] => Array
(
[v] => 0.00
[f] => $0.00
)
[3] => Array
(
[v] => 0.00
[f] => $0.00
)
[4] => Array
(
[v] => 0.00
[f] => $0.00
)
[5] => Array
(
[v] => 0.00
[f] => $0.00
)
)
)
[2] => Array
(
[c] => Array
(
[0] => Array
(
[v] => Tercero
[f] => TERCERO
)
[1] => Array
(
[v] => 0.00
[f] => $0.00
)
[2] => Array
(
[v] => 0.00
[f] => $0.00
)
[3] => Array
(
[v] => 4969.65
[f] => $4,969.65
)
[4] => Array
(
[v] => 0.00
[f] => $0.00
)
[5] => Array
(
[v] => 23695.39
[f] => $23,695.39
)
)
)
[3] => Array
(
[c] => Array
(
[0] => Array
(
[v] => Cuarto
[f] => CUARTO
)
[1] => Array
(
[v] => 0.00
[f] => $0.00
)
[2] => Array
(
[v] => 0.00
[f] => $0.00
)
[3] => Array
(
[v] => 0.00
[f] => $0.00
)
[4] => Array
(
[v] => 45974.86
[f] => $45,974.86
)
[5] => Array
(
[v] => 0.00
[f] => $0.00
)
)
)
[4] => Array
(
[c] => Array
(
[0] => Array
(
[v] => Quinto
[f] => QUINTO
)
[1] => Array
(
[v] => 0.00
[f] => $0.00
)
[2] => Array
(
[v] => 0.00
[f] => $0.00
)
[3] => Array
(
[v] => 1405.8
[f] => $1,405.80
)
[4] => Array
(
[v] => 39244
[f] => $39,244.00
)
[5] => Array
(
[v] => 0.00
[f] => $0.00
)
)
)
)
)
RecursiveFilterIterator
(part of the SPL)? \$\endgroup\$eval
that much, the question isn't "is there a better way", but "how many alternative ways are there, and how much better are they" ;-P \$\endgroup\$