PHP查找字符串值并通过数组中的位置对其进行标记 [英] PHP find string value and marked it by position in array

查看:139
本文介绍了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 ,而我的工作就是通过计算 59次 Giai1到Giai9 的字符来找到价值c>从 0 开始计数并获得该头寸的值,不包括字符-,因此如果 $ position = 59 ,则返回位置 58 的获取的值。

I have a int variable $position = 59, and my job is find value by counting characters from Giai1 to Giai9 for 59 times count from 0 and get value of this position not include character -, so if $position = 59 then the getted value at position 58 will return.

例如,在位置 20 处找到值,则在Giai4中返回14102处的 1(实际上是从0开始计数19)

For example, find value at position 20, the return is 1 at 14102 in Giai4 (actually 19 count from 0)

我已被编写此代码来完成

I've been wrote this code to do this

$position = 59;
$count = 0;
foreach ($data['result'][0] as $key => $item)
    {
        if(preg_match('@Giai@s', $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]);
}

预期结果将返回带有获得数字标记的数组。
例如,代码在位置 59(从0开始的58)处找到数字,并在 x 处进行了签名首先是 y 。您可以在 Giai5

Expected result will return this array with the mark of getted number. For example, code found number at position 59 (58 from 0) and signed it by x at first and y at end of value. You can see this in 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之后,我获得的职位价值总是错误的

from 1 to 50 it works fine, but after position 50, the value of position I get is always wrong

有什么想法吗?

推荐答案

如果我这次正确理解了您,则可以执行以下操作:

If I understood you correctly this time, you can do something like this:

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

这里是演示

代码有点冗长,但这是因为当您注意位置时会跳过破折号(-),但是当您需要标记字符时,必须将它们考虑在内。通过此代码,您可以了解算法,然后按所需方式重构代码。我建议从嵌套循环中退出,因为它们很难阅读。您可以通过将代码分解为函数或使用可用的数组函数来实现。

The code is a bit lengthy, but this is due to the fact that when you watch for the position you skip the dashes (-), but when you need to mark the character you have to take them into account. From this code you can understand the algorithm and then refactor code the way you want. I would suggest to escape from nested loops, as they are hard to read. You can do this by breaking code into functions or use the available array functions.

这篇关于PHP查找字符串值并通过数组中的位置对其进行标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆