2016-05-19 50 views
-1

我有這樣PHP查找字符串值,並將其標記的位置陣列

array:32 [▼ 
    "ID" => "7917" 
    "ProvinceCode" => "MB" 
    "Create" => "2016-05-18 18:16:26.790" 
    "DayOfTheWeek" => "4" 
    "Giai1" => "28192" 
    "Giai2" => "83509" 
    "Giai3" => "51911-02858" 
    "Giai4" => "14102-97270-96025-08465-89047-45904" 
    "Giai5" => "7892-9140-4069-8499" 
    "Giai6" => "6117-7471-5541-9119-4855-0566" 
    "Giai7" => "843-860-023" 
    "Giai8" => "71-13-55-89" 
    "Giai9" => "" 
    "Status" => "1" 
] 

數組我有一個int變量$position = 59,而我的工作是由0Giai1 to Giai9計數字符59 times計發現價值並且得到該位置的值不包括字符-,因此如果$position = 59則位置58處的得到的值將返回。

例如,在20位置發現價值,回報是1 at 14102 in Giai4 (actually 19 count from 0)

我已經寫了這個代碼來做到這一點

$position = 59; 
$count = 0; 
foreach ($data['result'][0] as $key => $item) 
    { 
     if(preg_match('@[email protected]', $key)) 
     { 
      $_item = str_replace('-', '', $item); 
      $count = $count + strlen($_item); 

      $chars = str_split($item); 
      $chars_sp = array_count_values($chars); 

      $countChar = count($chars); 
      if($count > $position) 
      { 
       //this block contains needed position 
       $math = $count - $position; 
       $secmath = strlen($_item) - $math; 
       for($i=$secmath;$i>=0;$i--){ 
        if($chars[$i] == '-'){ 
         $splash_last++; 
        } 
       } 

       $secmath = $secmath + $splash_last; 
       if($chars[$secmath] == '-'){ 
        echo "+1 - "; 
        $secmath = $secmath + 1; 
       } 

       echo "Count: $count Match: $math Secmatch: $secmath Splash_last: $splash_last"; 
       $chars[$secmath] = 'x' . $chars[$secmath] . 'y'; 

       $edited = implode('', $chars); 
       $data['result'][0][$key] = $edited; 
       break; 
      } 
     } 
    } 

    dd($data['result'][0]); 
} 

預期結果將返回數組與getted號的標記。 例如,在59 (58 from 0)位置找到的代碼爲x,在y結束時對其進行了標記。您可以在Giai5

//This is expected result 
//Result array with mark of value at needed position 

array:32 [▼ 
    "ID" => "7917" 
    "ProvinceCode" => "MB" 
    "Create" => "2016-05-18 18:16:26.790" 
    "DayOfTheWeek" => "4" 
    "Giai1" => "28192" 
    "Giai2" => "83509" 
    "Giai3" => "51911-02858" 
    "Giai4" => "14102-97270-96025-08465-89047-45904" 
    "Giai5" => "7892-9140-x4y069-8499" 
    "Giai6" => "6117-7471-5541-9119-4855-0566" 
    "Giai7" => "843-860-023" 
    "Giai8" => "71-13-55-89" 
    "Giai9" => "" 
    "Status" => "1" 
] 

從1到50,它工作正常看到這一點,但位置50後的位置我得到的價值永遠是錯的

任何想法?

+0

這是什麼點 - 你將如何使用這些標記? – splash58

+0

@ splash58很難告訴你爲什麼我需要那個,但我需要那個。 – vietnguyen09

回答

0

如果我理解正確的話,你這個時候,你可以做這樣的事情:

$array = [ 
    "ID" => "7917", 
    "ProvinceCode" => "MB", 
    "Create" => "2016-05-18 18:16:26.790", 
    "DayOfTheWeek" => "4", 
    "Giai1" => "28192", 
    "Giai2" => "83509", 
    "Giai3" => "51911-02858", 
    "Giai4" => "14102-97270-96025-08465-89047-45904", 
    "Giai5" => "7892-9140-4069-8499", 
    "Giai6" => "6117-7471-5541-9119-4855-0566", 
    "Giai7" => "843-860-023", 
    "Giai8" => "71-13-55-89", 
    "Giai9" => "", 
    "Status" => "1" 
]; 

$position = 59; 

$start = 0; 
$end = 0; 

foreach ($array as $key => &$value) { 
    if (!preg_match('/Giai/', $key)) { 
     continue;  
    } 

    $start = $end + 1; 
    $end = $start + strlen(str_replace('-', '', $value)) - 1; 

    if (($start <= $position) && ($position <= $end)) { 
     $counter = $start; 
     $value = str_split($value); 

     foreach ($value as &$char) { 
      if ($char === '-') { 
       continue; 
      } 

      if ($counter === $position) { 
       $char = "x{$char}y"; 
       break; 
      } 

      $counter++; 
     } 

     $value = join($value); 
    } 
} 

var_dump($array); 

這裏是demo

該代碼有點冗長,但這是因爲當您觀察位置時,您會跳過破折號(-),但是當您需要標記該角色時,必須將其考慮在內。從這段代碼你可以理解算法,然後按你想要的方式重構代碼。我建議逃脫嵌套循環,因爲他們很難閱讀。您可以通過將代碼分解爲函數或使用可用的數組函數來實現。

+0

我可以找到位置塊(根據位置,塊「Giai」包含所需的值),但重要的是計算該塊中值的位置的數學計算,將該值標記並返回原始值帶有標記值的數組。 – vietnguyen09

+0

你是什麼意思**標記值**? – sevavietl

+0

如果你發現從'Giai1到Giai8'的位置'59'的計數值在'Giai5'中,你可以找到提取值的一些方法是'4(example)',你用'x4y'標記它並更新'array [ 'Giai5']'這個標記。如果只找到哪個關鍵字按位置包含值,我不會在這裏發佈。 – vietnguyen09

0

您可以通過簡單的array_grep()獲取所有「Giai」鍵和implode以將值連接成一個大字符串,然後選擇該字符串的位置。

$giai = array_flip(preg_grep("/^Giai\d+$/", array_flip($a))); 
echo implode("",$giai)[$pos]; 

https://eval.in/573746

+0

我可以得到這個值,但我不能做的事情是標記找到的值並保持該數組格式。 – vietnguyen09

+0

我不確定你的意思是*「標記的已找到的值並保留該數組格式」*。你想如何標記找到的值? –

相關問題