有条件地替换数组的元素
substitute the elements of an array coditionally
我需要创建一个价格数组。所以我有一组基本价格和一组促销价格,如果促销价格低于基本价格,我把它放在新的价格数组中。为此,我写了这段代码:
<?php
$array_base_prices = array(
array('id'=>1, 'price'=>10),
array('id'=>2, 'price'=>2),
array('id'=>3, 'price'=>30),
array('id'=>4, 'price'=>40)
);
$array_promo = array(
array('id'=>1, 'price'=>20),
array('id'=>2, 'price'=>5),
array('id'=>3, 'price'=>2)
);
$base_prices_with_promo = array();
foreach ( $array_base_prices as $j => $u ) {
foreach ( $array_promo as $k => $v ) {
if ( ($v['id'] == $u['id']) && $u['price'] < $v['price']) {
$base_prices_with_promo[$j]['id'] = $array_base_prices[$j]['id'];
$base_prices_with_promo[$j]['price'] = $array_base_prices[$j]['price'];
}
if ( ($v['id'] == $u['id']) && $u['price'] > $v['price']) {
$base_prices_with_promo[$j]['id'] = $array_promo[$k]['id'];
$base_prices_with_promo[$j]['price'] = $array_promo[$k]['price'];
}
}
}
$base_prices_with_promo = array_replace( $array_base_prices, $base_prices_with_promo );
echo "<pre>";
print_r($base_prices_with_promo);
echo "</pre>";
?>
并且工作正常,但我认为嵌套 foreach
中的 if
条件有点乱。我正在使用多维关联数组,它们非常大,有很多键。所以我想知道是否有更好的替代方法来达到相同的结果。
我确实没有足够的上下文来确定这是否适合您,但根据您的小示例,我会将数组声明更改为:
$array_base_prices = array(
1 => 10,
2 => 2,
3 => 30,
4 => 40
);
$array_promo = array(
1 => 20,
2 => 5,
3 => 2
);
如果您需要存储比价格更多的数据,请使用数组:
$array_base_prices = array(
1 => array("price" => 10, "something_else" => null)
);
重点是将 id 作为父数组的索引。然后你的嵌套循环变成这样:
foreach ($array_base_prices as $id => $base_price) {
$base_prices_with_promo[$id] = $base_price;
if (isset($array_promo[$id]) && $base_price > $array_promo[$id]) {
$base_prices_with_promo[$id] = $array_promo[$id];
}
}
我需要创建一个价格数组。所以我有一组基本价格和一组促销价格,如果促销价格低于基本价格,我把它放在新的价格数组中。为此,我写了这段代码:
<?php
$array_base_prices = array(
array('id'=>1, 'price'=>10),
array('id'=>2, 'price'=>2),
array('id'=>3, 'price'=>30),
array('id'=>4, 'price'=>40)
);
$array_promo = array(
array('id'=>1, 'price'=>20),
array('id'=>2, 'price'=>5),
array('id'=>3, 'price'=>2)
);
$base_prices_with_promo = array();
foreach ( $array_base_prices as $j => $u ) {
foreach ( $array_promo as $k => $v ) {
if ( ($v['id'] == $u['id']) && $u['price'] < $v['price']) {
$base_prices_with_promo[$j]['id'] = $array_base_prices[$j]['id'];
$base_prices_with_promo[$j]['price'] = $array_base_prices[$j]['price'];
}
if ( ($v['id'] == $u['id']) && $u['price'] > $v['price']) {
$base_prices_with_promo[$j]['id'] = $array_promo[$k]['id'];
$base_prices_with_promo[$j]['price'] = $array_promo[$k]['price'];
}
}
}
$base_prices_with_promo = array_replace( $array_base_prices, $base_prices_with_promo );
echo "<pre>";
print_r($base_prices_with_promo);
echo "</pre>";
?>
并且工作正常,但我认为嵌套 foreach
中的 if
条件有点乱。我正在使用多维关联数组,它们非常大,有很多键。所以我想知道是否有更好的替代方法来达到相同的结果。
我确实没有足够的上下文来确定这是否适合您,但根据您的小示例,我会将数组声明更改为:
$array_base_prices = array(
1 => 10,
2 => 2,
3 => 30,
4 => 40
);
$array_promo = array(
1 => 20,
2 => 5,
3 => 2
);
如果您需要存储比价格更多的数据,请使用数组:
$array_base_prices = array(
1 => array("price" => 10, "something_else" => null)
);
重点是将 id 作为父数组的索引。然后你的嵌套循环变成这样:
foreach ($array_base_prices as $id => $base_price) {
$base_prices_with_promo[$id] = $base_price;
if (isset($array_promo[$id]) && $base_price > $array_promo[$id]) {
$base_prices_with_promo[$id] = $array_promo[$id];
}
}