2017-02-15 75 views
1

我有幾個需要與我的multidimantional數組進行比較的stings,並用newName替換並匹配oldName。在合併數組之前更改數組中的值

然後我需要將兩個字符串合併成一個新的字符串,只保留唯一的值(不包括'NA')。

所以,關於合併字符串的部分工作正常,但我不知道如何添加將檢查給定數組的部分。

$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"; 

$arr1 = explode(", ", $str1); 
$arr2 = explode(", ", $str2); 

$arr3 = array_diff(array_unique(array_merge($arr1, $arr2)), array('NA')); 

$str3 = trim(implode(", ", $arr3), ", "); 
+0

[由值PHP多維數組搜索]的可能的複製(http://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value) – miken32

回答

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" 
+0

謝謝。這對我來說很好。爲什麼它不優雅? – santa

+0

其實,我需要確保我檢查狀態和城市的情況下,有其他陣列具有相同的舊名稱... – santa

+0

IMO,它可以得到改善,原因:) – Wizard