2014-09-25 45 views
0

我不知道如何更好地說出我的問題,但這是我的情況。在數組中匹配字符串,修改字符串,然後創建新的數組

我有這樣的陣列執行以下操作:

$temp_array = array("111111-Name1-122874|876394|120972", "222222-Name2-122874|876394|120972", "333333-Name3-122874|876394|120972"); 

我需要循環通過此陣列,並嘗試每個串的所述第一部分的陣列

例如在匹配

$id   = "222222"; 
$rand_number = "999888"; 
if ($id match the first element in string) { 
    fetch this string 
    append "999888" to "122874|876394|120972" 
    insert this string back to array 
} 

所以,最終排列成爲

$temp_array = array("111111-Name1-122874|876394|120972", "222222-Name2-999888|122874|876394|120972", "333333-Name3-122874|876394|120972"); 

很抱歉,如果我的問題出現混亂,但它確實是相當困難對我來說,即使掌握一些必要的操作。

謝謝

+0

純粹基於上述 - foreach()循環,substr()匹配,然後創建一個新的輸出數組添加額外的數據。 – 2014-09-25 03:43:05

+2

追加還是前置?你說了一個,但你的例子顯示另一個? – 2014-09-25 03:52:15

+0

對不起,前置或追加實際上在我的情況下都很好 – user2028856 2014-09-25 03:53:40

回答

2

試試這個:

$temp_array = array("111111-Name1-122874|876394|120972", "222222-Name2-122874|876394|120972", "333333-Name3-122874|876394|120972"); 
$id   = "222222"; 
$rand_number = "999888"; 

// Loop over each element of the array 
// For each element, $i = the key, $arr = the value 
foreach ($temp_array as $i => $arr){ 

    // Get the first characters of the element up to the occurrence of a dash "-" ... 
    $num = substr($arr, 0, strpos($arr, '-')); 

    // ...and check if it is equal to $id... 
    if ($num == $id){ 
     // ...if so, add $random_number to the back of the current array element 
     $temp_array[$i] .= '|' . $rand_number; 
    } 
} 

輸出:

Array 
(
    [0] => 111111-Name1-122874|876394|120972 
    [1] => 222222-Name2-122874|876394|120972|999888 
    [2] => 333333-Name3-122874|876394|120972 
) 

See demo

注:由於袞在他的評論中指出的那樣,你的問題說:追加,但你的例子顯示了數據是前置。此方法附加,但可以根據需要進行更改。


http://php.net/manual/en/control-structures.foreach.php

http://php.net/manual/en/function.substr.php

http://php.net/manual/en/function.strpos.php

+0

感謝噸的答覆,我試過你的代碼,它的工作很好,但我真的很想更好地理解此代碼。你能提供一點解釋嗎?謝謝:) – user2028856 2014-09-25 03:56:34

+0

@ user2028856我已經給代碼添加了一些評論,可能有助於解釋一點。 – 2014-09-25 04:01:57

+0

@ user2028856查看最新更新和新演示 – 2014-09-25 04:11:00

2

原始答案 - 它將取決於初始ID的預期值。如果他們能可長可短,然後爆炸,而不是使用SUBSTR

$temp_array = array("111111-Name1-122874|876394|120972","222222-Name2-122874|876394|120972","333333-Name3-122874|876394|120972"); 

    $id = "222222"; 
    $rand_number = "999888"; 

    foreach($temp_array as $t){ 
     if(substr(0,6,$t)==$id){ 
      $new[] = $t.'|'.$rand_number; 
     }else{ 
      $new[] = $t; 
     } 
    } 
2

你也可以使用在這種情況下,一些爆炸過的連字符:

$temp_array = array("111111-Name1-122874|876394|120972", "222222-Name2-122874|876394|120972", "333333-Name3-122874|876394|120972"); 
$id = "222222"; 
$rand_number = "999888"; 
foreach($temp_array as &$line) { 
        //^reference 
    $pieces = explode('|', $line); // explode pipes 
    $first = explode('-', array_shift($pieces)); // get the first part, explode by dash 
    if($first[0] == $id) { // if first part is equal to id 
     $first[2] = $rand_number; // replace the third part with random 
     $first = implode('-', $first); // glue them by dash again 
     $line = implode('|', array($first, implode('|',$pieces))); // put them and glue them back together again 
    } 
} 
echo '<pre>'; 
print_r($temp_array); 
+0

此代碼也很好,除了我不想取代第三部分是隨機的,我只是想將新的隨機數追加到現有的隨機數字串 – user2028856 2014-09-25 03:58:45

+0

@ user2028856實際上,看着結果數組,看起來這部分被隨機取代,所以真正的意圖是當匹配發生時追加/推送它結束 – Ghost 2014-09-25 04:00:52

2

使用array_walk

$temp_array = array("111111-Name1-122874|876394|120972", "222222-Name2-122874|876394|120972", "333333-Name3-122874|876394|120972"); 

$id   = "222222"; 
$rand_number = "999888"; 
$params = array('id'=>$id, 'rand_number'=>$rand_number); 
array_walk($temp_array, function(&$value, $key, $param){ 
    $parts = explode('-', $value); // Split parts with '-' so the first part is id 
    if ($parts[0] == $param['id']){ 
     $parts[2]="{$param['rand_number']}|{$parts[2]}"; //prepend rand_number to last part 
     $value=implode('-',$parts); //combine the parts back 
    } 
},$params); 

print_r($temp_array); 

如果你只是想添加的代碼另一個版本變得更短

$params = array('id'=>$id, 'rand_number'=>$rand_number); 
array_walk($temp_array, function(&$value, $key, $param){ 
    // here check if the first part of the result of explode is ID 
    // then append the rand_number to the value else append '' to it. 
    $value .= (explode('-', $value)[0] == $param['id'])? "|{$param['rand_number']}" : ''; 
},$params); 

編輯:添加到代碼的評論。

相關問題