Reputation: 2373
I have an array like this
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"
]
I have a int variable $position = 59
, and my job is find value by counting characters from Giai1 to Giai9
for 59 times
and get value of this position not include character -
, so I've been wrote this code
$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]);
}
from 1 to 50 it works fine, but after position 50, the value of position I get is always wrong.
Any idea?
Upvotes: 0
Views: 73
Reputation: 3802
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;
$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];
}
This is more "functional approach". Firstly you filter array to get array only containing Giai*
keys (be aware that this will work only in PHP >= 5.6). You can read more about array_filter()
. Then you reduce this array to one string with array_reduce()
. Next check if the position is valid and return the character if it is.
Here is demo.
Upvotes: 0
Reputation: 4508
This should work :
$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];
Upvotes: 1