我需要与我的multidimantional数组进行比较,并使用newName替换和匹配oldName。
然后我需要将两个字符串合并为一个新字符串,只保留唯一值(不包括' NA')。
因此,关于合并字符串的部分工作正常,但我不确定如何添加将检查给定数组的部分。
row_Renamed("ImageCol") = IO.File.ReadAllBytes(row_Renamed("memberPhoto"))
答案 0 :(得分:0)
不是那么优雅的解决方案:(
<?php
$area = array(
array(
'state' => "TX",
'city' => "Austin",
'newName' => "SoCo (S. Congress Ave.)",
'oldName' => "South Congress"
),
array(
'state' => "NY",
'city' => "Buffalo",
'newName' => "Elmwood Village",
'oldName' => "Elmwood "
),
);
$state = "TX";
$city = "Austin";
$str1 = "78704 (South Austin), SoCo (S. Congress Ave.), Bouldin Creek";
$str2 = "South Congress";
list($search, $replace) = array_reduce($area,function($carry,$area_item) {
$carry[0][]=trim($area_item['oldName']);
$carry[1][]=trim($area_item['newName']);
return $carry;
}, array());
$result = str_replace($search, $replace, implode(', ',[$str1,$str2]));
$arr1 = explode(", ", $result);
$arr3 = array_diff(array_unique($arr1), array('NA'));
$str3 = trim(implode(", ", $arr3), ", ");
var_dump($str3);
var_dump()结果:
string(60) "78704 (South Austin), SoCo (S. Congress Ave.), Bouldin Creek"
更新并评论了以下代码
<?php
// source data array
$area = array(
array(
'state' => "TX",
'city' => "Austin",
'newName' => "SoCo (S. Congress Ave.)",
'oldName' => "South Congress"
),
array(
'state' => "NY",
'city' => "Buffalo",
'newName' => "Elmwood Village",
'oldName' => "Elmwood "
),
);
/**
* @param string $state
* @param string $city
* @param string|array of strings $strings
* @param array $areas
*/
function process($state, $city, $strings, $areas) {
// get rid off parameters extra spaces at the beginning and at the end of string
$state = trim($state);
$city = trim($city);
// find data to replace
$replaceData = array_reduce($areas, function($carry, $area) use ($state, $city) {
// if no state selected, then any state will be processed
// else we're searching only for selected state
$needToReplace = ($state === '') ? true : $state == trim($area['state']);
// the same for the city
$needToReplace &= ($city === '') ? true : $city == trim($area['city']);
if ($needToReplace) {
$carry['search'][] = trim($area['oldName']);
$carry['replace'][] = trim($area['newName']);
}
return $carry;
}, array('search'=>array(), 'replace'=>array()));
// if parameter $strings is an array, merge it into the plain string
$string = is_array($strings) ? implode(', ', $strings) : $strings;
// do the replace
$result = str_replace($replaceData['search'], $replaceData['replace'], $string);
// convert result string into an array, and trim all the values
$tmpArray = array_map(function($value){
return trim($value);
},explode(', ', $result));
// exclude 'NA', select the unique values and join all into the final result string
return implode(', ', array_diff(array_unique($tmpArray), array('NA')));
}
// replace any state, any city
$result = process('', '', array(
'78704 (South Austin), SoCo (S. Congress Ave.), Bouldin Creek',
'South Congress'
), $area);
var_dump($result);
// replace located only in NY/Austin
$result = process('NY', 'Austin',
'78704 (South Austin), SoCo (S. Congress Ave.), Bouldin Creek, South Congress',
$area);
var_dump($result);
结果:
string(60) "78704 (South Austin), SoCo (S. Congress Ave.), Bouldin Creek"
string(76) "78704 (South Austin), SoCo (S. Congress Ave.), Bouldin Creek, South Congress"