我有這兩個陣列,我需要用 $columns 的索引替換 $products 的數字索引。
我不知道用“foreach 回圈”或其他 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"
),
);
在回圈中,我遍歷兩個陣列以替換索引。
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);
這是結果:
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
)
)
uj5u.com熱心網友回復:
聽起來這就是你想要的
$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);
你的版本的問題是你沒有在回圈內創建一個干凈的陣列,所以缺失的值會從上一次迭代中保留下來。
uj5u.com熱心網友回復:
您應該使用空值開始嵌套 foreach 的每次迭代 $columnsArray
請注意,您正在回圈產品陣列,并使用這些數字鍵直接索引到$columnas陣列中。
如果產品多于列,這可能會導致未定義的陣列鍵。
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);
輸出
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
)
)
uj5u.com熱心網友回復:
<?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);
uj5u.com熱心網友回復:
$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);
您可以在此處查看輸出并進行測驗
uj5u.com熱心網友回復:
它可以在沒有嵌套回圈或 if 陳述句的情況下完成。
$products = array_map(function (array $product) use ($columns): array {
$product = array_pad($product, count($columns), null);
return array_combine($columns, $product);
}, $products);
或者,如果您寧愿省略丟失的鍵而不是攜帶空占位符:
$products = array_map(function (array $product) use ($columns): array {
$columns = array_slice($columns, 0, count($product));
return array_combine($columns, $product);
}, $products);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/395526.html
