簡體   English   中英

PHP在數組中查找字符串位置

[英]PHP Find string position in array

我有這樣的數組

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 Giai1 to Giai9找到值,並且獲得該位置的值(不包含字符- ,因此我已經寫了這段代碼

$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之后,我得到的排名值總是錯誤的。

任何想法?

這應該工作:

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

您可以執行以下操作:

$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> = 5.6 )。 您可以閱讀有關array_filter()更多信息。 然后,使用array_reduce()將數組簡化為一個字符串。 接下來檢查位置是否有效,如果正確,則返回字符。

這是演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM