I have some trouble re-building a PHP array. I need to change it for printing a table to a PDF doc with tfPDF Library.
I tried it with foreach() loops, array_walk(), array_walk_recursive(), array_chunk() and array_slice() - so far without success.
This is my array $data:
$data = [
"file_id" => 1394,
"user_id" => 463466,
"periode" => 2022,
"costs" => 64.45,
"values" => [
"457" => [
"1" => [
"data_id" => 1,
"supplier_id" => 457,
"costs1" => 1000,
"costs2" => 100,
"group_name" => "7%",
],
"140" => [
"data_id" => 140,
"supplier_id" => 457,
"costs1" => 2000,
"costs2" => 50,
"group_name" => "19%",
],
"197" => [
"data_id" => 197,
"supplier_id" => 457,
"costs1" => 3000,
"costs2" => 300,
"group_name" => "special",
],
],
"430" => [
"490" => [
"data_id" => 490,
"supplier_id" => 430,
"costs1" => 500,
"costs2" => 30,
"group_name" => "new 4",
],
"552" => [
"data_id" => 552,
"supplier_id" => 430,
"costs1" => 7000,
"costs2" => 250,
"group_name" => "new 5",
],
],
"425" => [
"1106" => [
"data_id" => 1106,
"supplier_id" => 425,
"costs1" => 10,
"costs2" => 4,
"group_name" => "new 6",
],
],
],
];
For the print function the following format would be the best:
$pdf->Row(array(
"data_id \n" .
"data_id \n" .
"data_id",
"supplier_id \n" .
"supplier_id \n" .
"supplier_id",
"costs1 \n" .
"costs1 \n" .
"costs1",
"costs2 \n" .
"costs2 \n" .
"costs2",
"group_name \n" .
"group_name \n" .
"group_name",
));
So I need to change the $data to an array like this:
[
'file_id' => 1394,
'user_id' => 463466,
'periode' => 2022,
'costs' => 64.45,
'values' => [
457 => [
'data_id' => '1, 140, 197',
'supplier_id' => '457, 457, 457',
'costs1' => '1000, 2000, 3000',
'costs2' => '100, 50, 300',
'group_name' => '7%, 19%, special',
],
430 => [
'data_id' => '490, 552',
'supplier_id' => '430, 430',
'costs1' => '500, 7000',
'costs2' => '30, 250',
'group_name' => 'new 4, new 5',
],
425 => [
'data_id' => 1106,
'supplier_id' => 425,
'costs1' => 10,
'costs2' => 4,
'group_name' => 'new 6',
],
],
];
Can you help me?
The last step would be the comma separation like this:
array_walk($data['values'], function (&$val) {
$val[] = implode(", ", $val[]);
});
A few loops and testings and it should work: