2016-05-19 18 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

我不确定你的意思是*“标记的已找到的值并保留该数组格式”*。你想如何标记找到的值? –

相关问题