如何通过传入ID更改数组中的字段值
例如,read_excel
是$ cityID = '2';
,对于特维尔市,应将UF_DEFAULT
更改为,对于莫斯科市,应将$ cityID = '10' ;
更改为0
或不存在UF_DEFAULT
,请保留所有内容而不覆盖$cityID = '2'; // 2 - есть | 10 - нет
$arr = array(
'1'=>array('ID'=>'1', 'NAME'=>'Moscow', 'UF_DEFAULT'=>'1', 'UF_CITY_CORD'=>'55.76, 37.64'),
'2'=>array('ID'=>'2', 'NAME'=>'Tver', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
'3'=>array('ID'=>'3', 'NAME'=>'Nahodka', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
'4'=>array('ID'=>'4', 'NAME'=>'Omsk', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
);
foreach($arr as $key => $arSection)
{
$arSectionsResult[$arSection['ID']] = $arSection;
}
unset($key, $arSection);
echo "<pre>";print_r($arSectionsResult);echo "</pre>";
* Adds Author, World and Series information to the shop loop above the “Add to cart” button
**/
add_action( 'woocommerce_after_shop_loop_item', 'shop_display_author_and_world',9);
function shop_display_author_and_world() {
global $product;
// Find custom fields
$authorname = get_post_meta( get_the_ID(), 'product_author_name.post_title', true );
$authorurl = get_post_meta( get_the_ID(), 'product_author_name.permalink', true );
$world = get_post_meta( get_the_ID(), 'product_world.post_title', true );
$worldurl = get_post_meta( get_the_ID(), 'product_world.permalink', true );
$series = get_post_meta( get_the_ID(), 'product_series', true );
$seriesnum = get_post_meta( get_the_ID(), 'product_series_number', true );
// Add these fields to the shop loop if set
if ( ! empty ( $series ) ) {
echo '<div><a style="color:#54595F;font-weight:bold" href="'. $authorurl .'">'. $authorname . '</a><br><a style="color:#E12021;" href="'. $worldurl .'">'. $world . '</a><br><span style="color:#54595F; font-variant:small-caps">'. $series . ', book ' . $seriesnum .'</span></div>';
}
else {
echo '<div><a style="color:#54595F;font-weight:bold" href="'. $authorurl .'">'. $authorname . '</a><br><a style="color:#E12021;" href="'. $worldurl .'">'. $world . '</a></div>' ;
}}
无论如何都要感谢
答案 0 :(得分:1)
$cityID = '10'; // 2 - есть | 10 - нет
$arr = array(
'1'=>array('ID'=>'1', 'NAME'=>'Moscow', 'UF_DEFAULT'=>'1', 'UF_CITY_CORD'=>'55.76, 37.64'),
'2'=>array('ID'=>'2', 'NAME'=>'Tver', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
'3'=>array('ID'=>'3', 'NAME'=>'Nahodka', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
'4'=>array('ID'=>'4', 'NAME'=>'Omsk', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
);
// first we find - if we should change UF_DEFAULT
$changeDefault = false;
foreach ($arr as $item) {
// find item with same ID and UF_DEFAULT != 1
if ($item['ID'] === $cityID && $item['UF_DEFAULT'] != 1) {
$changeDefault = true;
break;
}
}
// if you have to `changeDefault` - iterate over
// array again and change according to `cityID`
if ($changeDefault) {
foreach ($arr as &$item) {
$item['UF_DEFAULT'] = $item['ID'] == $cityID ? 1 : 0;
}
}
print_r($arr);
答案 1 :(得分:0)
即使我不太了解问题背后的逻辑,这也是实现此目的的一种方法。
首先检查给定密钥是否存在array_key_exists。如果是这样,请将除给定ID之外的所有UF_DEFAULT设置为0。
if (array_key_exists($cityId, $arr)) {
foreach ($arr as $k => $city) {
$arr[$k]["UF_DEFAULT"] = 0;
}
$arr[$cityId]["UF_DEFAULT"] = 1;
} else {
// Key not existent...
}
答案 2 :(得分:0)
仅执行一次循环。仅更改需要更改的值。尽快退出迭代。
我正在循环中“通过引用修改输入数组”。为避免使用$row
发布问题下标,我在循环结束时将其取消设置。
代码:(Demo)
$cityID = '2';
$oldCityFound = false;
$newCityFound = false;
$arr = [
'1' => ['ID'=>'1', 'NAME'=>'Moscow', 'UF_DEFAULT'=>'1', 'UF_CITY_CORD'=>'55.76, 37.64'],
'2' => ['ID'=>'2', 'NAME'=>'Tver', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'],
'3' => ['ID'=>'3', 'NAME'=>'Nahodka', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'],
'4' => ['ID'=>'4', 'NAME'=>'Omsk', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'],
];
foreach ($arr as &$row) {
if ($row['ID'] == $cityID) {
if ($row['UF_DEFAULT'] == 1) {
break; // nothing to change
}
$row['UF_DEFAULT'] = 1;
$newCityFound = true;
} elseif ($row['UF_DEFAULT'] == 1) {
$row['UF_DEFAULT'] = 0;
$oldCityFound = true;
}
if ($newCityFound && $oldCityFound) {
break; // both jobs done ...efficiently
}
}
unset($row); // best practice
var_export($arr);
在我的回答中,我假设不信任外键作为ID值。如果外键始终等于其中的ID值,则可以在循环之前执行两个快捷键:
if (!isset($arr[$cityID])) {
// cityID not found, now what?
}
和
if ($arr[$cityID]['UF_DEFAULT'] == 1) {
// nothing to change
}