PHP在数组中查找字符串位置

时间:2016-05-19 11:39:23

标签: php arrays

我有一个像这样的数组

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,我的工作是通过计算Giai1 to Giai9 59 times的字符来查找值,并获得此位置的值不包括字符-,所以我已经写了这段代码

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

从1到50它工作正常,但在第50位之后,我得到的位置值总是错误的。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

这应该有效:

$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 = 29;

$str = '';

foreach ($array as $key => $value) {
  if(preg_match('@Giai@s', $key)) {
     $str .= str_replace('-', '', $value);
  }
}

echo $str[$position + 1];

答案 1 :(得分: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;

$giai = array_reduce(
    array_filter(
        $array,
        function ($key) {
            return preg_match('/Giai/', $key);
        },
        ARRAY_FILTER_USE_KEY
    ), 
    function ($giai, $elem) {
        return $giai . str_replace('-', '', $elem);
    },
    ''
);

if ($position <= strlen($giai)) {
    echo $giai[$position - 1];
}

这是更多“功能方法”。首先,您过滤数组以获取仅包含Giai*个键的数组(请注意,这仅适用于PHP&gt; = 5.6 )。您可以阅读有关array_filter()的更多信息。然后使用array_reduce()将此数组缩减为一个字符串。接下来检查位置是否有效,如果是,则返回该字符。

这是demo