I have these 2 arrays and I need to replace the numeric indices of $products with the indices of $columns.
I don't know if it is correct to solve it with "foreach loop" or with another PHP function.
PROBLEM: the returned array must have the same number of elements as the initial one.
$columnas = array( "CATEGORIA", "PRODUCTO", "IMAGEN", "PRECIO", "DESCUENTO", "DISPONIBLE");
$products = array(
array(
"Burgers", "Doble con queso", "burger.jpg","300"
),
array(
"Burgers", "Triple", "burger_triple.jpg", "350", "10%", "si"
),
array(
"Burgers", "Triple2", "burger_triple.jpg", "380"
),
array( "Burgers", "Triple3"
),
);
In the loop I go through both arrays to replace the indexes.
foreach( $products as $k => $product ) {
foreach($product as $t => $y){
$columnsArray[$columnas[$t]] = $y;
// $product[$columnas[$t]] = $y; // Other option
}
$newProducts[] = $columnsArray;
}
print_r($newProducts);
This is the result:
Array
(
[0] => Array
(
[CATEGORIA] => Burgers
[PRODUCTO] => Doble con queso
[IMAGEN] => burger.jpg
[PRECIO] => 300
)
[1] => Array
(
[CATEGORIA] => Burgers
[PRODUCTO] => Triple
[IMAGEN] => burger_triple.jpg
[PRECIO] => 350
[DESCUENTO] => 10%
[DISPONIBLE] => si
)
[2] => Array
(
[CATEGORIA] => Burgers
[PRODUCTO] => Triple2
[IMAGEN] => burger_triple.jpg
[PRECIO] => 380
[DESCUENTO] => 10% <------- should not be
[DISPONIBLE] => si <------- should not be
)
[3] => Array
(
[CATEGORIA] => Burgers
[PRODUCTO] => Triple3
[IMAGEN] => burger_triple.jpg <------- should not be
[PRECIO] => 380 <------- should not be
[DESCUENTO] => 10% <------- should not be
[DISPONIBLE] => si <------- should not be
)
)
CodePudding user response:
Sounds like this is what you're after
$data = [];
foreach ($products as $k => $product) {
$result = [];
foreach ($columnas as $i => $key) {
$result[$key] = (isset($product[$i]) ? $product[$i] : null);
}
$data[] = $result;
}
print_r($data);
The problem with your version was that you weren't creating a clean array inside the loop, so missing values would remain from the previous iteration.
CodePudding user response:
You should start each iteration of the nested foreach with an empty $columnsArray
Note that you are looping a products array, and using those numerical keys to directly index into the $columnas
array.
This can cause an undefined array key if there are more products than columns.
foreach( $products as $k => $product ) {
$columnsArray = [];
foreach($product as $t => $y){
if (array_key_exists($t, $columnas)) {
$columnsArray[$columnas[$t]] = $y;
}
}
$newProducts[] = $columnsArray;
}
print_r($newProducts);
Output
Array
(
[0] => Array
(
[CATEGORIA] => Burgers
[PRODUCTO] => Doble con queso
[IMAGEN] => burger.jpg
[PRECIO] => 300
)
[1] => Array
(
[CATEGORIA] => Burgers
[PRODUCTO] => Triple
[IMAGEN] => burger_triple.jpg
[PRECIO] => 350
[DESCUENTO] => 10%
[DISPONIBLE] => si
)
[2] => Array
(
[CATEGORIA] => Burgers
[PRODUCTO] => Triple2
[IMAGEN] => burger_triple.jpg
[PRECIO] => 380
)
[3] => Array
(
[CATEGORIA] => Burgers
[PRODUCTO] => Triple3
)
)
CodePudding user response:
<?php
$columnas = array( "CATEGORIA", "PRODUCTO", "IMAGEN", "PRECIO", "DESCUENTO", "DISPONIBLE");
$products = array(
array(
"Burgers", "Doble con queso", "burger.jpg","300"
),
array(
"Burgers", "Triple", "burger_triple.jpg", "350", "10%", "si"
),
array(
"Burgers", "Triple2", "burger_triple.jpg", "380"
),
array( "Burgers", "Triple3"
),
);
$newProducts = [];
foreach ($products as $p) {
// slice out the same number of keys from $columnas, combine into the new array
$newProducts[] = array_combine(array_slice($columnas,0,count($p)),$p);
}
print_r($newProducts);
CodePudding user response:
$result = array_reduce($products, function($accumulator, $item) use ($columnas){
if(count($item) < count($columnas)){
$item = array_pad($item, count($columnas),NULL);
}
$accumulator[] = array_combine($columnas,$item);
return $accumulator;
},[]);
print_r($result);
You can see outputs and test this here
CodePudding user response:
It can be done without nested loops or if-statements.
$products = array_map(function (array $product) use ($columns): array {
$product = array_pad($product, count($columns), null);
return array_combine($columns, $product);
}, $products);
Or, if you'd rather have the missing keys omitted rather than carry a null placeholder:
$products = array_map(function (array $product) use ($columns): array {
$columns = array_slice($columns, 0, count($product));
return array_combine($columns, $product);
}, $products);