K
As I see it, and as I understand, you need your arrays from the beginning, to combine into one only that you can handle to paint them in a specific table. I see you can do it in two ways:Using https://www.php.net/manual/en/function.array-combine.php Knowing that you have different arrays between yourself and need to join them, you can use array_combine as follows:You create a new array with the keys you need, in my case I plant it like this:$keys = ['id', 'tipo_compra', 'fecha_compra', 'concepto', 'debe', 'haber', 'saldo'];
You combine your arrays with the keys as follows:$id_comp = [8, 8, 8, 8, 8];
$tipo_comp = ['Egreso', 'Egreso', 'Egreso', 'Egreso', 'Egreso', 'Egreso'];
$fecha_comp = ['2021-10-09', '2021-10-09', '2021-10-09', '2021-10-09', '2021-10-09', '2021-10-09'];
$concepto = ['Sueldo Agosto 2021 Juan Pinto Guerra', 'Sueldo Agosto 2021 Jerson Carrillo Salas', 'Sueldo Agosto 2021 Juan Obando Carcamo', 'Sueldo Agosto 2021 Douglas Medel Perez', 'Sueldo Agosto 2021 Cristian Leiva Argel', 'Sueldo agosto 2021 Rodrigo Mora bello'];
$debe = ['1.672.432', '407.117', '1.244.522', '852.207', '1.513.820', '756.544'];
$haber = [0, 0, 0, 0, 0, 0];
$saldo = ['1.672.432', '2.079.549', '3.324.071', '4.176.278', '5.690.098', '6.446.642'];
$data = array_combine($keys, [ $id_comp, $tipo_comp, $fecha_comp, $concepto, $debe, $haber, $saldo ]);
This should result in something like this: [
"id" => [
8,
8,
8,
8,
8,
],
"tipo_compra" => [
"Egreso",
"Egreso",
"Egreso",
"Egreso",
"Egreso",
"Egreso",
],
"fecha_compra" => [
"2021-10-09",
"2021-10-09",
"2021-10-09",
"2021-10-09",
"2021-10-09",
"2021-10-09",
],
"concepto" => [
"Sueldo Agosto 2021 Juan Pinto Guerra",
"Sueldo Agosto 2021 Jerson Carrillo Salas",
"Sueldo Agosto 2021 Juan Obando Carcamo",
"Sueldo Agosto 2021 Douglas Medel Perez",
"Sueldo Agosto 2021 Cristian Leiva Argel",
"Sueldo agosto 2021 Rodrigo Mora bello",
],
"debe" => [
"1.672.432",
"407.117",
"1.244.522",
"852.207",
"1.513.820",
"756.544",
],
"haber" => [
0,
0,
0,
0,
0,
0,
],
"saldo" => [
"1.672.432",
"2.079.549",
"3.324.071",
"4.176.278",
"5.690.098",
"6.446.642",
],
]
Having an arrangement like the one I show you, you should be able to make use of foreach nests to go through what you need and extract the data:foreach($data as $key => $value) {
echo $key."\n";
echo "--------------\n";
foreach($value as $key => $val) {
echo $val."\n";
}
echo "--------------\n";
}
Having a result like this:id
8
8
8
8
8
tipo_compra
Egreso
Egreso
Egreso
Egreso
Egreso
Egreso
fecha_compra
2021-10-09
2021-10-09
2021-10-09
2021-10-09
2021-10-09
2021-10-09
concepto
Sueldo Agosto 2021 Juan Pinto Guerra
Sueldo Agosto 2021 Jerson Carrillo Salas
Sueldo Agosto 2021 Juan Obando Carcamo
Sueldo Agosto 2021 Douglas Medel Perez
Sueldo Agosto 2021 Cristian Leiva Argel
Sueldo agosto 2021 Rodrigo Mora bello
debe
1.672.432
407.117
1.244.522
852.207
1.513.820
756.544
haber
0
0
0
0
0
0
saldo
1.672.432
2.079.549
3.324.071
4.176.278
5.690.098
6.446.642
Using https://www.php.net/manual/en/function.array-merge.php To make use of the array_merge, you must modify your given arrays a bit, and add them to the arrays. keys you need to identify them, as I put it in the example:$id_comp = ['id' => [8, 8, 8, 8, 8]];
$tipo_comp = ['tipo_compra' => ['Egreso', 'Egreso', 'Egreso', 'Egreso', 'Egreso', 'Egreso']];
$fecha_comp = ['fecha_compra' => ['2021-10-09', '2021-10-09', '2021-10-09', '2021-10-09', '2021-10-09', '2021-10-09']];
$concepto = ['concepto' => ['Sueldo Agosto 2021 Juan Pinto Guerra', 'Sueldo Agosto 2021 Jerson Carrillo Salas', 'Sueldo Agosto 2021 Juan Obando Carcamo', 'Sueldo Agosto 2021 Douglas Medel Perez', 'Sueldo Agosto 2021 Cristian Leiva Argel', 'Sueldo agosto 2021 Rodrigo Mora bello']];
$debe = ['debe' => ['1.672.432', '407.117', '1.244.522', '852.207', '1.513.820', '756.544']];
$haber = ['haber' => [0, 0, 0, 0, 0, 0]];
$saldo = ['saldo' => ['1.672.432', '2.079.549', '3.324.071', '4.176.278', '5.690.098', '6.446.642']];
Then make use of the array_merge as follows:$data = array_merge($id_comp, $tipo_comp, $fecha_comp, $concepto, $debe, $haber, $saldo);
And by making use of the foreach anit of the previous example, you would get the same resultI'll leave you. http://sandbox.onlinephpfunctions.com/code/9e7dd2832b49dc37109e54c6de6027155a9e5281