gpt4 book ai didi

PHP 用其他数组中的值替换数组索引键

转载 作者:行者123 更新时间:2023-12-05 01:54:51 24 4
gpt4 key购买 nike

<分区>

我有这两个数组,我需要用 $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
)

)

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com